Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Declarations →  Declaration Statements →  Data Types and Data Objects →  Declaring Data Types →  TYPES 

TYPES - REF TO

Short Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


TYPES ref_type { {TYPE REF TO type} 
               | {LIKE REF TO dobj} }.

Effect

The addition REF TO specifies a data type for a reference variable. The entry behind REF TO specifies the static type of the reference variable. The static type restricts the object quantity to which a reference variable can refer. The dynamic type of a reference variable is the data type and the object class to which it refers. The static type is always more general or the same as the dynamic type (see also assignment rules for reference variables).

Apart from data and object, generic data types can be specified after TYPE REF TO.

Types for Data Reference Variables

If, for type, the predefined generic data type data was specified, a data type for a data reference variable of the static type data is created. Such reference variables can refer to any data objects.

If type is specified as any non-generic data type (meaning a non-generic data type from ABAP Dictionary or from the public visibility section of a global class), as a non-generic local program type already defined using TYPES, or as a non-generic predefined ABAP type, a data type is created for a data reference variable with an appropriate static type. Such reference variables can refer to all data objects of the same type.

Für die Angabe von dobj gilt das Gleiche wie bei TYPES ... LIKE. A data type is created for a data reference variable and the static type of this variable is inherited from the data type of the specified data object. Such reference variables can refer to all data objects of the same type.

A data reference variable can be dereferenced in different ways, depending on its static type:

  • A data reference variable typed generically with TYPE REF TO data can be dereferenced only in the statement ASSIGN with the dereferencing operator ->*.
  • A data reference variable typed completely with TYPE REF TO complete_type or LIKE REF TO dobj can be dereferenced in all matching operand positions with the dereferencing operator ->*. If the static data type is structured, the object component selector enables access to the components of the structure with dref->comp.

Types for Data Reference Variables

If, for type, the predefined generic data type data was specified, a data type for a data reference variable of the static type data is created. Such reference variables can refer to instances of any class.

If, for type, a global or local class was specified, a data type for a class reference variable whose static type is the specified class is created. Such reference variables can refer to all instances of the class and its subclasses. A class reference variable can be used to access all components of an object. If the static type is the superclass of the dynamic type, the components that can be called statically are a subset of the components of the dynamic type. All attributes and methods of a dynamic type can be called using a class reference variable and the dynamic object attribute call (see ASSIGN) and the dynamic method call (see CALL METHOD).

If, for type, a global or local interface was specified, a data type for an interface reference variable whose static type is the specified interface is created. Such reference variables can refer to objects of all classes that implement the interface. When calling components of objects with interface references, the name of the static type is always implicitly placed before the component. An interface reference variable can be used to access only those interface components of an object that are known in the static type. This applies to both dynamic and static access.


Notes

  • The TYPE addition can be used to define data types for data and object reference variables. Only data types for data reference variables can be defined using the LIKE addition.
  • The predefined generic data type any (as is the case with all other generic types apart from data) can only be specified after TYPE but not after REF TO.

Example

This example shows how data types are defined for an interface reference variable and for a class reference variable as well as a data reference to an interface reference variable.

INTERFACE i1. 
... 
ENDINTERFACE. 

CLASS c1 DEFINITION. 
  PUBLIC SECTION. 
    INTERFACES i1. 
ENDCLASS. 

TYPES: iref TYPE REF TO i1, 
       cref TYPE REF TO c1, 
       dref TYPE REF TO iref.