ABAP - Keyword Documentation →  ABAP Programming Guidelines →  Robust ABAP →  Dynamic Programming Techniques → 

Accessing Data Objects Dynamically

Background

Field symbols and data references are used to access data objects whose name and attributes are not known until runtime.

Field symbols and data references are closely linked because only completely typed data reference variables can be dereferenced in any operand position. Completely generic data reference variables (REF TO data) can be dereferenced in the ASSIGN statement only.

Data reference variables can be declared in the same context as all other data objects, especially also as attributes of classes. Field symbols, in contrast, can only be declared within procedures (methods) or in the global declaration part. However, the latter is no longer allowed.

Rule

Use field symbols and data references in appropriate ways

Use field symbols and data references for the purpose that best matches their semantics:

Details

Both field symbols and data references can be understood as pointers to memory areas. The main difference is in the different access semantics.

Note

Actually, data reference variables can be better used in programs that are based on ABAP Objects because they have the same semantics as object reference variables and therefore represent a more modern programming concept. Field symbols, on the other hand, provide more functions than data references and can thus not always be replaced by them. Consequently, the usage of field symbols for dynamic accesses to data objects is still recommended, although the sole use of data references would be preferred for consistency and simplicity reasons.

Bad Example

The following source code shows a loop for an internal table in which the system is supposed to directly access the current row. If a generic data reference variable is used for this purpose, a field symbol is also needed for its dereferencing.

METHOD some_method.
  "IMPORTING i_itab TYPE INDEX TABLE
  DATA dref TYPE REF TO data.
  FIELD-SYMBOLS <fs> TYPE data.
  ...
  LOOP AT i_itab REFERENCE INTO dref.
    ASSIGN dref->* TO <fs>.
    <fs> = ...
  ENDLOOP.
  ...
ENDMETHOD.

Good Example

The following source code simplifies the above example by using a field symbol, which is required to access table rows anyway, directly and without using a data reference. The direct use of the field symbol thus also complies with the KISS principle.

METHOD some_method.
  "IMPORTING i_itab TYPE INDEX TABLE
  FIELD-SYMBOLS <fs> TYPE data.
  ...
  LOOP AT i_itab ASSIGNING <fs>.
    <fs> = ...
  ENDLOOP.
...
ENDMETHOD.