Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Assignments →  Setting References 

GET REFERENCE

Short Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


GET REFERENCE OF dobj INTO dref. 

Effect

This statement gets a reference to a data object dobj and places it into the reference variable dref. The reference variable dref must be declared as a data reference variable. The static type of the data reference variable must be more general than or the same as the data type dobj, according to the conversion rules for reference variables.

The data object is specified directly and in accordance with the rules described in the section Data Objects in Operand Positions. If you have an offset/length specification, the data type dobj here must not be string or xstring.


Notes

  • The GET REFERENCE statement and the addition REFERENCE INTO are the only options available to statements for internal tables to generate stack references. Stack referencescan become invalid if the referenced data object is deleted.
  • When applied to data objects in the heap GET REFERENCE creates memory-retaining heap references.
  • The content of two reference variables filled with GET REFERENCE is only the same if the remaining administration information is the same, apart from the referenced data objects. For example, a reference that is retrieved directly by specifying the data object is not the same as a reference that is retrieved by specifying a field symbol if this has a different data type due to a casting.
  • When an internal table has a header line, a data reference variable can only point to this or the table body. In the GET REFERENCE statement, the name of an internal table with a header line addresses the header line. To address the table body, [] must be appended to the name in the usual way. A dereferenced data reference to which a table body is assigned behaves in the same way in operand positions as a table without a header line.
  • The context in which a data reference is retrieved using GET REFERENCE determines whether the referenced data object can be changed. If the data object can be changed in the context of GET REFERENCE, it can be changed at any position, also using the reference. For example, a reference to a private attribute of a class can be retrieved in a method, and passed to external destinations. Since the attribute can be changed in the class, it can also be changed outside of the class using the reference. Conversely, a constant or a non-changeable input parameter can also be made non-changeable by passing on its reference.
  • A data reference retrieved using GET REFERENCE that references a data object in the shared objects memory can also be stored in a closed area instance version. The restrictions described for the addition AREA HANDLE of the statement CREATE DATA must not be ignored.

Example

Creating data references to the individual characters of a data object text and storing them in an internal table. Direct dereferencing at an operand position is possible because the data reference is fully typed.

TYPES c1 TYPE c LENGTH 1. 

DATA: dref     TYPE REF TO c1, 
      dref_tab LIKE TABLE OF dref. 

DATA: text TYPE c LENGTH 10 VALUE '0123456789', 
      off  TYPE i. 

DO 10 TIMES. 
  off = sy-index - 1. 
  GET REFERENCE OF text+off(1) INTO dref. 
  APPEND dref TO dref_tab. 
ENDDO. 

LOOP AT dref_tab INTO dref. 
  WRITE / dref->*. 
ENDLOOP. 

Exceptions


Non-Catchable Exceptions

  • Cause: The data object specified after INTO is not a reference variable.
    Runtime Error: GET_REF_REFERENCE_EXPECTED
  • Cause: GET REFERENCE is not permitted for a substring.
    Runtime Error: GET_REF_SUBSTRING_NOT_ALLOWED