ABAP Keyword Documentation → ABAP - Reference → Declarative statemnts → Data Types and Data Objects → Declaring Data Objects → DATA
DATA - REF TO
Other versions: 7.31 | 7.40 | 7.54
Syntax
DATA ref { {TYPE REF TO type}
| {LIKE REF TO dobj} }
[VALUE IS INITIAL]
[READ-ONLY].
Effect
You use the REF TO
addition to declare reference variable ref
.
The entry after REF TO
specifies the static type of the reference variables.
The static type limits the set of objects to which ref
can point. The dynamic
type of a reference variable is the data type or the class to which it currently points. The static type is always more general or the same as the dynamic type (also see the conversion rules for reference variables).
The syntax and meaning of the TYPE
and LIKE
additions
are exactly the same as the definition of reference types in section TYPES
-REF TO
, but here they are used to generate a bound reference type.
Only IS INITIAL
can be specified as a
start value after the VALUE
addition.
Note
Reference variables are opaque, that is, you cannot access their content directly. A reference is made up of the address of an object and other administration information.
Example
In this example, an object reference (oref
) and two data references
(dref1
and dref2
) are declared. Both data references
are completely typed and can be dereferenced using the dereferencing operator ->*
at operand positions.
CLASS c1 DEFINITION.
PUBLIC SECTION.
DATA a1 TYPE i VALUE 1.
ENDCLASS.
DATA: oref TYPE REF TO c1,
dref1 LIKE REF TO oref,
dref2 TYPE REF TO i.
CREATE OBJECT oref.
GET REFERENCE OF oref INTO dref1.
CREATE DATA dref2.
dref2->* = dref1->*->a1.