Ansicht
Dokumentation

ABENCONVERSION_REFERENCES - CONVERSION REFERENCES

ABENCONVERSION_REFERENCES - CONVERSION REFERENCES

ABAP Short Reference   BAL Application Log Documentation  
This documentation is copyright by SAP AG.
SAP E-Book

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 the same as the dynamic type of the source reference variable. After a successful assignment, the target reference variable points to the same object as the source reference variable, that is, the target reference variable inherits the dynamic type of the source reference variable.

Note

Reference variables can point to objects in the internal session or in the shared objects memory. While 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 it is a read or write access). 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 the object contains.
  • The static type is determined in 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. This basic rule determines all assignments between reference variables.

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. The same applies to a non-initial reference variable that contains an invalid reference that no longer points to an object.

Example

The object reference variable oref has the static type super_class. After the object is generated with NEW, its dynamic type is the special class sub_class - as shown by RTTI.

Upcast and Downcast

In an assignment between reference variables, the target variable inherits 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.

Upcast

If the static type of the target variables is less specific or the same as the static type of the source variable, an assignment is always possible. The name upcast comes from the fact that the movement within the inheritance tree is upwards. Since the target variable can have more dynamic types in comparison to the source variables, this assignment is also referred to as a widening cast. An upcast 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 regular assignment operator (=), the insertion of lines 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 upcast in combination with inline declarations, however, can be a good way of providing a reference variable with a more general type.

Example

See the previous example. The statement iref = NEW class( ) is an upcast, because it is followed by an reference variable of the static type of a superclass that points to an object of a subclass.

Downcast

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, whether it is less specific or the same as the dynamic type of the source variable. The name downcast comes 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 referred to as a narrowing cast. A downcast must always be performed explicitly. The following options are available:

  • Use of the addition INTO of the statement WHEN TYPE in a case distinction with CASE TYPE OF for object reference variables.

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

Notes

  • For non-initial source variables, the predicate expression IS INSTANCE OF or the case distinction CASE TYPE OF can be used to check whether a downcast is possible in specific classes or interfaces.
  • The dynamic type of an initial or invalid source variable is undefined. In a downcast, the null reference or an invalid reference that does not point to an object can be assigned to every target variable that can be specified here.
  • When a subclass is instantiated, an associated object reference variable cannot be cast to the subclass until the instance constructors of all superclasses have been executed. When an instance constructor is executed, the dynamic type of the object reference variable is the current superclass.

Example

The following example shows the three ways of performing a downcast for object reference variables.

  • If the casting operator ?= is used, the exception CX_SY_MOVE_CAST_ERROR is caught if a downcast is not possible.
  • If the casting operator CAST is used, the predicate expression IS INSTANCE OF is used in advance to check whether the downcast is possible.
  • The use of the addition INTO of the statement WHEN TYPE in a case distinction with CASE TYPE OF combines the check and the downcast.

In each of the three cases, oref is assigned to the class reference variable that is reached first. A check with IS INSTANCE OF would of course also be possible before the use of the casting operator ?=.






TXBHW - Original Tax Base Amount in Local Currency   Fill RESBD Structure from EBP Component Structure  
This documentation is copyright by SAP AG.

Length: 10283 Date: 20240425 Time: 174154     sap01-206 ( 176 ms )