Simple field definition

DATA - Simple Field Definition

Variants:
1. DATA f.
2. DATA f(len).

Additions:
1. ... TYPE type
2. ... LIKE f1
3. ... TYPE LINE OF itabtype
4. ... LIKE LINE OF itab
5. ... TYPE REF TO cif
6. ... VALUE lit
7. ... DECIMALS n
8. ... READ-ONLY

Effect

The internal program field f is created with standard length. If you do not specify a reference type (addition 1 - addition 5), the system creates a field with type C.

The field name f can be up to 30 characters long. Apart from the
special characters '(', ')', '+', '.', ',', ':' you can use any other character, including digits. However, a name may not consist only of digits.
SPACE is a reserved name, and you may not use it. You should also not use a field in a statement with a name identical to one of the additional parameters (for example, do not use PERFORM SUB USING CHANGING).

A type name may be up to 30 characters long. Apart from the special characters '(', ')', '+', '.', ',', ':', '-', '<', '>', you may use all other characters, including digits. However, a type name may not consist entirely of digits.

Recommendations for type names:

Effect

The field f is created with type type. For the type, you can specify either one of the predefined types listed below, or a type defined using the TYPES statement.
The standard length (SL) of a field depends on its type.

Type Explanation SL Initial value

C Text (Character) 1 space
N Numeric text 1 '00...0'
D Date (YYYYMMDD) 8 '00000000'
T Time (HHMMSS) 6 '000000'
X Hexadecimal (HeX code) 1 X'00'
I Integer 4 0
P Packed number 8 0
F Floating point number 8 0

Example

DATA NUMBER TYPE I.

The field NUMBER is created with type I. You can now use it in the program. In particular, you can assign numeric values to the field and use it in calculations ( ABAP number types).

Effect

Field f is created with the same field attribtues as the data object f1, which has already been declared. This is allowed for any data object (field, parameter, structure...) as long as its type has been fully specified.
f1 can be any ABAP Dictionary reference.

Example

DATA TABLE_INDEX LIKE SY-TABIX.

The field TABLE_INDEX now has the same attribues as SY-TABIX (index field for internal tables).

Note

You should use this addition whenever you can. If the type of a field to which you are referring changes, the ABAP runtime system updates all references automatically. It also stops the system from carrying out unnecessry (and maybe undesirable) type conversions.

Effect

The specified type itabtype must have the type of an internal table. The system creates a data object with the same line type of the specified table.

Example

TYPES TAB_TYPE TYPE I OCCURS 10. 
DATA  TAB_WA   TYPE LINE OF TAB_TYPE. 

The data object TAB_WA now has the same attributes as a line of the table type TAB_TYPE, that is, type I.

Effect

The data object itab must be an internal table with or without header line. The system creates a data objec with the same line type as the specified table.

Example

DATA TAB	TYPE I OCCURS 10. 
DATA TAB_WA LIKE LINE OF TAB.

The data object TAB_WA now has the same attributes as a line of the table TAB, that is, type I.

Effect

The data object f is defined as a reference variable for the object or interface cif. Classes and interfaces form the basis of ABAP Objects, the object-oriented extension of ABAP

. The fact that f is a reference is emphasized in the REF TO addition. A referenec variable with type cif can only point to an instance with the same type:
An object variable can only point to instances (objects) of the class cif.
An interface variable can point to all instances of all classes that have implemented the interface cif.

Note

Objects (instances of classes) are only addressed using object or interface variables. For information about creating instances of classes, see CREATE OBJECT.

Example

INTERFACE I_TEST. 
  METHODS METH. 
ENDINTERFACE. 
 
CLASS C_TEST DEFINITION. 
 PRIVATE SECTION. 
   INTERFACES I_TEST. 
ENDCLASS. 
 
DATA: O_TEST_1 TYPE REF TO C_TEST, 
	O_TEST_2 TYPE REF TO C_TEST, 
	IF_TEST  TYPE REF TO I_TEST. 
 
CREATE OBJECT O_TEST_1. 
 
O_TEST_2 = O_TEST_1. 
IF_TEST  = O_TEST_1. 
 
CLASS C_TEST IMPLEMENTATION. 
  METHOD I_TEST~METH. 
	... 
  ENDMETHOD. 
ENDCLASS.

Effect

The field f is not assigned its usual initial value (see table above). Instead, it has the initial value lit. You may enter a constant. Instead of a literal, you can also specify IS INITIAL. In this case, the value of the field is set to its initial value. This form is particularly necessary in conjunction with the CONSTANTS statement, since you must always use VALUE with it.

Example

DATA: NUMBER	TYPE I		VALUE 123, 
	FLAG					VALUE 'X', 
	TABLE_INDEX LIKE SY-TABIX VALUE 45.

The field NUMBER with type I is created, and does not have an initial value of 0, but 123; the new field FLAG with type C (length 1) contains the value 'X'; TABLE_INDEX has the value 45, since SY-TABIX is a numeric field.

Effect

This addition is only necessary for field type P. When you calculate using this field or output it, it has n decimal places, where n is between 0 and 14.

When you create a new program, "floating point arithmetic" is selected by default. If you deselect this attributes, the DECIMALS specification is only effective when you output the field, not when you use it in calculations. In this case, you must ensure that you have the correct number of decimal places by multiplying or dividing the field by the appropriate power of 10. ( COMPUTE).

You are recommended always to work with the fixed point arithmetic switched on. This ensures that intermediated results (division!) are calculated with the greatest possible accuracy (31 decimal places).
To see whether fixed point P or floating point F is more suitable, see " ABAP number types"

Effect

You can only use this addition in the ( PUBLIC SECTION) of a class definition ( CLASS - ENDCLASS ) or in an interface definition ( INTERFACE - ENDINTERFACE).

The READ-ONLY addition makes an attribute declared in a DATA statement visible to all users, but only methods of the class or its subclasses may change it. In other words, the attribute can be read as though it were PUBLIC, but it is PROTECTED when it comes to changing it.

Additions:
As in variant 1

Effect

The field f is created with length len.
You can only use this variant for fields with type C, N, P and X . Fields with a different type can only be created with their standard length (see the table under variant 1).
The permitted length of a field depends on its type:

Type Permitted length
C 1 - 65535
N 1 - 65535
P 1 - 16
X 1 - 65535

Note

The system can display one character or two decimal or hexadecimal digits per byte. In P fields, one character is reserved for the sign. This means that a P field with length 3 can contain up to 5 digits, but an X field with length 3 can contain 6. Both have output length 6.