Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing Internal Data →  Assignments →  Assigning References →  Setting Reference Variables 

GET REFERENCE

Short Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


GET REFERENCE OF dobj INTO dref. 

Effect

This statement sets the reference in the reference variable dref in a way that makes it point to the data object dobj. The following can be specified for dref:

  • An existing 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 assignment rules for reference variables.
  • An inline declaration DATA(var). This declares a data reference variable whose static type is the data type of dobj. The data type of dobj must be statically recognizable as complete or as the generic type data. Field symbols and formal parameters with other generic types, in particular any, are not possible.

The data object is specified directly and in accordance with the rules described in the section Reader Positions. If offsets/lengths (+off(len)) are specified, the data type dobj here cannot be string or xstring.


Notes

  • Alongside the reference operator REF and the addition REFERENCE INTO, the statement GET REFERENCE is the only option available to statements for internal tables to create stack references. Stack references can 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 statement GET REFERENCE, 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.
  • If references are set using GET REFERENCE, permission to access the data object in question is only checked at the position of the statement. After that, the references can be passed on to any destination and the associated data objects can be accessed from any position using the references. To prevent access to private and read-only attributes using references outside classes, do not publish references to these attributes externally. A constant or read-only input parameter, however, can never be made modifiable by passing 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 be respected.
  • The reference operator REF functions like the statement GET REFERENCE and can be used in general expression positions.

Example

Creates data references to the individual characters of a data object text and saves them in an internal table. Direct dereferencing at an operand position is possible because the data reference is fully typed. After the output, an alternative implementation with an iteration expression and the instance operator NEW is shown that has the same result.

TYPES c1 TYPE c LENGTH 1. 

DATA: dref     TYPE REF TO c1, 
      dref_tab     LIKE TABLE OF dref WITH EMPTY KEY, 
      dref_tab_new LIKE dref_tab. 

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. 
  cl_demo_output=>write( |{ dref->* }| ). 
ENDLOOP. 

cl_demo_output=>display( ). 

dref_tab_new = VALUE #( 
  FOR j = 0 UNTIL j > 9 ( REF #( text+j(1) ) ) ). 
ASSERT dref_tab_new = dref_tab. 

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: ILLEGAL_SUBSTRING_PARAMETER