Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Creating Objects and Values →  CREATE DATA →  CREATE DATA - REF TO 

Creating Reference Variables

This example demonstrates how reference variables are created.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA intf_name TYPE string.
    DATA cls_name  TYPE string.
    DATA dref TYPE REF TO data.

    FIELD-SYMBOLS <ref>  TYPE any.
    FIELD-SYMBOLS <attr> TYPE any.

    intf_name = '\PROGRAM=DEMO_CREATE_REFERENCE\INTERFACE=INTF'.
    CREATE DATA dref TYPE REF TO (intf_name).
    ASSIGN dref->* TO <ref>.

    cls_name = '\PROGRAM=DEMO_CREATE_REFERENCE\CLASS=CLS'.
    CREATE OBJECT <ref> TYPE (cls_name).

    ASSIGN ('<REF>->ATTR') TO <attr>.
    cl_demo_output=>display_data( <attr> ).

Description

Creates an interface reference variable dynamically. The absolute type name is used for the name of the local interface.

The reference variable is assigned to a field symbol <ref> using dereferencing. According to the general typing rules, the field symbol must either be typed in full generically or typed with reference to the interface intf.

The dynamically created reference variable is used to create and address an object of a class. Since this field symbol is fully generic, only the display variant of the dynamic ASSIGN (and not a special dynamic access) can be used to access the interface attribute.