Show TOC

PointersLocate this document in the navigation structure

Concept

Pointers (data objects whose content points to other objects) are implemented in ABAP using field symbols and reference variables.

Field Symbols

A field symbol is a symbolic name declared using the statement FIELD-SYMBOLS or inline using the operator FIELD-SYMBOL( ... ) to which a physical memory area is assigned at runtime using the statement ASSIGN or similar additions. The assignment defines the data type used to handle the memory area (this is known as casting). Like a data object, a field symbol can be used in any operand position and works with the content of the referenced memory area (value semantics).

Example

Assigns the memory area of a text field text to a field symbol <hex>. This performs a casting to the type of the field symbol. The pointy brackets are a fixed component of the name.

               DATA text TYPE c LENGTH 10 VALUE 'Hallo!'.

FIELD-SYMBOLS <hex> TYPE x.

ASSIGN text TO <hex> CASTING.

WRITE <hex>.
            

Reference Variable

A reference variable is a data object that contains a reference. Both data reference variables and object reference variables exist. The static type of a reference variable (its data type, class, and interface) defines the object to which it can point. Reference variables are a prerequisite for making instantiations using CREATE DATA or CREATE OBJECT or with the constructor operator NEW( … ). Assignments between reference variables copy the reference (reference semantics). A component is accessed using oref->comp. Data objects created using CREATE DATA are called anonymous data objects, since they can only be addressed using data reference variables.

Data objects or class objects created using CREATE or NEW( ... ) cannot be deleted again explicitly. Instead, the associated reference variables are canceled using CLEAR. The garbage collector removes any objects no longer referenced automatically. The system class cl_abap_weak_reference can be used to create weak object references. Class objects that are only referenced by weak object references can also be removed by the garbage collector.

Example

Declares a data reference variable dref and creates an anonymous data object with type i. Both CREATE and NEW( ... ) deliver the same result. Addresses the data object by dereferencing using the operator ->*.

               DATA dref TYPE REF TO i.

CREATE DATA dref.
dref = NEW #( ).

dref->* = 111.