SAP NetWeaver AS ABAP Release 740, ©Copyright 2014 SAP AG. All rights reserved.
ABAP Keyword Documentation → ABAP − Reference → Declarations → Declaration Statements → Classes and Interfaces → ABAP Objects - Overview → Objects →Object References
Object references are used as pointer objects. Object references (the content of object reference variables) are the only way to access the components of objects in an ABAP program. References can be used to access attributes and methods, but not events.
Object Reference Variables
Object reference variables contain object references. An object reference variable oref is either initial or contains a reference to an existing object. In the latter case, the predicate expression oref IS BOUND is true. An object reference variable that points to an object contains the 'address' of the object. Objects and their components can only be addressed through object references.
Reference variables are data objects with a deep data type. A reference variable can be defined as the smallest indivisible unit of complex data objects such as structures and internal tables. When handling reference variables or complex data objects that contain reference variables, note the special features that apply for deep types.
Static Type of Object Reference Variables
Object reference variables can be created either as class reference variables or interface reference variables.
Class Reference Variables
Interface Reference Variables
As well as in declarations, the addition TYPE REF TO ... can be used in all other statements in which types can be specified, for example to specify the types of interface parameters in procedures.
Special Object Reference Variables
Within a class there are two specific references, the self-reference me and the pseudo reference super.
Initializes Object Reference Variables
Like other variables, object reference variables are also initialized using the statement CLEAR. The initial value of an object reference variable is a reference that does not point to any object.
Assigning Object References Using Up Cast
Object references can be assigned between object reference variables. This means that the references in several reference variables can point to the same object (sharing). If assignments are made between reference variables, the dynamic type of the source variable must be compatible with the static type of the target variable. The dynamic type is the class of the object to which the reference in a reference variable points. The static type is the type (class or interface) with which the reference variable has been typed. The static type of the target variable must always be more general than the dynamic type of the source variable.
The syntax check in the program can only compare the static type of the source variable with the static type of the target variable. If reference variables are assigned using the assignment operator (=), the system performs this comparison and assignments can only be made that meet the above requirements for static types (up cast). The same applies when passing parameters to procedures. Assignments of this kind are always possible if the static type of the target variable can address the same number of components as the static type of the source variable or fewer components.
If cref1 and cref2 are class reference variables,
and iref1 and iref2 are interface reference variables, the following assignments can be checked statically:
object is a predefined empty class and the root node for all inheritance trees in ABAP Objects. This class has no components and is the generic type of all object reference variables. Reference variables with the type object can be used as containers for passing references. They cannot be used for static access to objects. Dynamic access is possible.
Assigning Object References Using Down Cast
In all cases where a static type check is not possible, and in all cases where type checks are not performed until program runtime, an explicit down cast is necessary. The following make this possible:
The syntax requires a down cast to be used in all cases that are not listed above for static type checks.
Example
Object References as Actual Parameters
Object references can be passed as actual parameters to procedures (methods, function modules, and subroutines) by specifying object reference variables. As far as typed formal parameters are concerned, only actual parameters of exactly the same type can be passed if the formal parameter can be modified in the procedure. This applies also to EXPORTING and CHANGING parameters of function modules and methods, parameters in subroutines passed by reference and CHANGING parameters in subroutines passed by value.
Passing actual parameters to formal parameters that corresponds to an up cast, such as passing a class reference variable to a formal parameter which is typed with reference to an interface of the class or one of its superclasses, is not possible if the parameter can be changed in the procedure.
The reason is that a reference could be assigned to the actual parameter in the procedure which is incompatible with its static type, such as a reference to any other class that implements the same interface or a reference to a subclass in another path of the inheritance tree.
Assigning Object References to Field symbols
When object references are assigned to typed field symbols, the same applies as when passing a reference to typed formal parameters: The types must be identical. Otherwise, incompatibilities between dynamic and static types may occur. For example, if two class reference variables of different classes implementing the same interface one after the other are assigned to the same field symbol, an inconsistent state is produced.