Skip to content

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

Assignment Rules for Reference Variables

The content of a reference variable can only be assigned to another reference variable. At the same time,

No conversion takes place when variables are assigned. For an assignment to take place, the static type of the target reference variable must be more general than or match the dynamic type of the source reference variable. If the assignment is successful, the target reference variable points to the same object as the source reference variable, meaning that the target reference variable inherits the dynamic type of the source reference variable.

Other versions: 7.31 | 7.40 | 7.54


Note

Reference variables can point to objects in the internal session or in the shared objects memory. An area handle is used to bind an area instance version to a program, however references from the program can be made to shared objects and vice versa (if these are read or write references). In other cases, an area instance version is closed, which means that no references can point to the internal session from an area instance version. References that point from the internal session to shared objects can be saved, but not dereferenced. Object references and data references are possible within a completed area instance version, although data references are subject to restrictions with respect to the dynamic type.

Static Type and Dynamic Type

Each reference variable has a dynamic type and a static type.

  • The dynamic type is defined at runtime of the program, and is the data type of the data object or the class of the object to which the reference variable points. It determines the components contained in the object.
  • The static type is set with the declaration of the reference variable. In data references, the static type is either a non-generic data type or the predefined generic type data. In object references, the static type is either a class or an interface, so an object reference can also be referred to as a class reference or an interface reference.

The static type of a reference variable is always less specific or the same as the dynamic type.


Note

An initial reference variable contains the null reference, which does not point to any objects. This means that it does not have a data type or a class as a dynamic type.

Up Cast and Down Cast

In an assignment between reference variables, the target variable adopts the dynamic type of the source variable. An assignment is possible if the static type of the target variable is less specific or the same as the dynamic type of the source variables.

Up Cast

If the static type of the target variables is less specific or the same as the static type of the source variable, assignment is always possible. The name up cast arises from the fact that the movement within the inheritance space is upwards. Since the target variable can accept more dynamic types in comparison to the source variables, this assignment is also known as a widening cast. An up cast is possible in all ABAP statements in which the content of a data object is assigned to another data object. This includes, for example, assignments with the normal assignment operator (=), the insertion of rows in internal tables, or passes from actual to formal parameters.

Explicit castings using the casting operator ?= are also possible, but not usually necessary. Using the casting operators CAST for an up cast in combination with inline declarations, however, can be a good way of providing a reference variable with a more general type.

Down Cast

If the static type of the target variable is more specific than the static type of the source variable, a check must be made at runtime (before the assignment is executed) to see whether it is less specific or the same as the dynamic type of the source variable. The name down cast arises from the fact that the movement in the inheritance space is downwards. Since the target variable can accept fewer dynamic types in comparison to the source variable, this assignment is also known as a narrowing cast. A down cast must always be performed explicitly. The following options are available:

If this prerequisite is not met, a handleable exception is raised and the reference variable keeps its original value after the assignment.


Note

The dynamic type of an initial source variable is undefined. The null reference can be assigned to every target variable in a down cast that can be specified here.

Continue

Assignments between Data Reference Variables

Assignments between Object Reference Variables