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

=, ?=, Up Cast und Down Cast

Syntax

destination_ref =|?= source_ref.

Effect

Assignment between two reference variables. The reference in source_ref is assigned destination_ref. After a successful assignment, destination_ref points to the same object as source_ref (reference semantics). The assignment of reference variables is a special form of assignments of data objects, whereby two assignment operators are available for assignments between reference variables and are used in accordance with the assignment rules for reference variables:

The same applies to the right side and left side as when assigning data objects, with the following restrictions:

Hints

Example

The first two assignments in the following source code section are up casts:

The next two assignments are down casts:

CLASS c1 DEFINITION INHERITING FROM object.
ENDCLASS.

CLASS c2 DEFINITION INHERITING FROM c1.
ENDCLASS.

CLASS c3 DEFINITION INHERITING FROM c2.
ENDCLASS.

DATA oref  TYPE REF TO object.
DATA oref1 TYPE REF TO c1.
DATA oref2 TYPE REF TO c2.
DATA oref3 TYPE REF TO c3.

oref1 = NEW c2( ).

oref = oref1.

IF oref IS INSTANCE OF c2.
   oref2 ?= oref.
ENDIF.

TRY.
    oref3 ?= oref2.
  CATCH cx_sy_move_cast_error.
    ...
ENDTRY.

Exceptions

Catchable Exceptions

CX_SY_MOVE_CAST_ERROR