ABAP Keyword Documentation → ABAP - Reference → Assignments → Assignment Rules
Assignment Rules for Reference Variables
The content of a reference variable can be assigned only to another reference variable. Data references can be assigned only to data reference variables and object references only to object reference variables. 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 dynamic type of an initial data reference variable is the predefined generic type
data
. The dynamic type of an initial object reference variable is the predefined generic typeobject
.
- 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.
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 variable.
Up Cast
If the static type of the target variable 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 you are moving upwards
within the inheritance space. As the target variable can accept more dynamic types in comparison to
the source variable, this assignment is also know 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.
Down Cast
If the static type of the target variable is more specific than the static type of the source variable,
you must check at runtime before the assignment is executed, 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 you are moving
downwards in the inheritance space. Because 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 is only possible
using the special assignment operator ?=
(casting operator) or the statement
MOVE ... ?TO ...
. If this prerequisite is not met, a handleable exception is raised and the reference variable keeps its original value.