CREATE OBJECT ... .
1. CREATE OBJECT cref.
2. CREATE OBJECT ref TYPE class.
3. CREATE OBJECT ref TYPE (class).
Creates an object, that is, the instance of a
class, in ABAP Objects. The additions
CREATE PUBLIC|PROTECTED|PRIVATE of the statement CLASS determine who is allowed to create objects of a class.
CREATE OBJECT cref.
1. ... EXPORTING
p1 = f1 ... pn = fn
2. ... EXCEPTIONS except1 = rc1 ... exceptn = rcn
cref is a reference variable that must be typed with reference
to a class. Reference variables are typed using the TYPE
REF TO addition of the DATA
or TYPES statements. You cannot create reference variables with reference to an
interface using this variant of the CREATE OBJECT statement because interfaces do not have instances.
An instance of the class is created using the CREATE OBJECT
cref statement and is then used to type cref.
The instance exists in the same context as the current internal mode and the reference in cref points to this instance.
Since you can copy references between reference variables of the same type, (using the
MOVE statement), the reference in the reference variable
cref does not necessarily point to the object that was generated using the CREATE OBJECT cref statement.
The reference variable cref does not have to be initial
before the CREATE OBJECT statement is executed. If there
is already a reference to an object in cref, the reference
to the new object replaces the reference to the previous object. If the previous reference was the last reference to its object, the latter is deleted by the Garbage Collection.
If a class contains an instance constructor (the special method
CONSTRUCTOR), the CREATE OBJECT statement calls this method after the object is generated completely. If a class contains a
static constructor (the special static method
CLASS_CONSTRUCTOR), and this has not yet been executed in the program, the CREATE OBJECT statement calls this method before the object is generated. See also
Visibility of Instance Constructors.
CLASS C1 DEFINITION.
PUBLIC SECTION.
DATA A.
ENDCLASS.
TYPES: T_C1 TYPE REF TO C1.
DATA: CREF1 TYPE T_C1,
CREF2 LIKE CREF1.
CREATE OBJECT: CREF1,
CREF2.
CREF1->A = 'X'.
CREF2->A = 'Y'.
...
CREF1 = CREF2.
...
The DATA statement is used to create two reference variables CREF1
and CREF2 with reference to the class
C1. CREATE OBJECT creates two different instances
of the class C1, to which the references in the reference
variables will point. After the CREF1 = CREF2 assignment,
CREF1 also contains a reference to the second object.
Since there is no longer a reference to the first object, this is automatically deleted by the system (Garbage Collection).
... EXPORTING p1 = f1 ... pn = fn
If the EXPORTING addition is used, appropriately typed actual parameters f1...fn must be passed to all IMPORTING parameters p1 ... pn of the instance constructor of the class, provided they have not been declared optional.
See the example in Addition 2, EXCEPTIONS.
... EXCEPTIONS except1 = rc1 ... exceptn = rcn
You can use the optional EXCEPTIONS addition to handle exceptions in the
instance constructor of the class. Exceptions are triggered by the
RAISE except and
MESSAGE RAISING statements in the CONSTRUCTOR method.
An exception except is handled by being placed in the EXCEPTIONS list. It is thus linked to the return code rc. If an exception is triggered, the system stops executing the CONSTRUCTOR method, deletes the instance that has just been created, and assigns the return code rc to the system variable SY-SUBRC.
If an exception triggered by RAISE except is not handled, the program currently running is aborted.
If an exception triggered by MESSAGE RAISING is not handled, the message specified is sent and the system proceeds according to the message type.
CLASS C1 DEFINITION.
PUBLIC SECTION.
METHODS CONSTRUCTOR IMPORTING P1 TYPE I
EXCEPTIONS EX1.
...
ENDCLASS.
DATA NUMBER TYPE I.
DATA O TYPE REF TO C1.
...
NUMBER = 10.
...
CREATE OBJECT O EXPORTING P1 = NUMBER
EXCEPTIONS EX1 = 4.
IF SY-SUBRC = 4.
WRITE / 'Exception in constructor'.
ENDIF.
CLASS C1 IMPLEMENTATION.
METHOD CONSTRUCTOR.
IF P1 > 5.
RAISE EX1.
ENDIF.
...
ENDMETHOD.
ENDCLASS.
In this example, the class C1 contains an instance constructor
with IMPORTING and EXCEPTIONS
parameters. The constructor is aborted if an exception is triggered and consequently a particular class-specific condition arises. The relevant object is then deleted and the reference variable initialized.
CREATE OBJECT ref TYPE class.
1. ... EXPORTING
p1 = f1 ... pn = fn
2. ... EXCEPTIONS except1 = rc1 ... exceptn = rcn
If you use this variant of the CREATE OBJECT statement, you create an instance of the class that is explicitly declared as a class. ref can be any reference variable that can point to the class declared. You can also use class references that are typed with reference to the class class or its superclasses, and interface references that are typed with reference to an interface that class has implemented.
... EXPORTING p1 = f1 ... pn = fn
As variant 1.
... EXCEPTIONS except1 = rc1 ... exceptn = rcn
As variant 1.
INTERFACE I1.
...
ENDINTERFACE.
CLASS C1 DEFINITION.
PUBLIC SECTION.
INTERFACES I1.
ENDCLASS.
DATA IREF TYPE REF TO I1.
CREATE OBJECT IREF TYPE C1.
In this example an instance of the class C1 is created, to which the interface reference IREF points. The class implements the interface I1, with which IREF is typed.
CREATE OBJECT ref TYPE (class).
1. ... EXPORTING
p1 = f1 ... pn = fn
2. ... EXCEPTIONS except1 = rc1 ... exceptn = rcn
3. ... PARAMETER-TABLE itab
4. ... EXCEPTION-TABLE itab
As variant 2, except that the name name of the class is declared dynamically as the content of the field class. At runtime, the class name from the field class is inserted in the statement and handled as if it were static. In particular, local classes obscure identically-named global classes in the dynamic variant as well.
If the class is declared dynamically, you can also specify absolute type names.
... EXPORTING p1 = f1 ... pn = fn
... EXCEPTIONS except1 = rc1 ... exceptn = rcn
... PARAMETER-TABLE itab
... EXCEPTION-TABLE itab
As with the above variants and with the dynamic method call,
these additions fill the IMPORTING parameters of the instance constructor statically or dynamically and handle the classic exceptions.
INTERFACE I1.
...
ENDINTERFACE.
CLASS C1 DEFINITION.
PUBLIC SECTION.
INTERFACES I1.
ENDCLASS.
CLASS C2 DEFINITION.
PUBLIC SECTION.
...
ENDCLASS.
CLASS C3 DEFINITION.
PUBLIC SECTION.
METHODS CREATE_OBJECT IMPORTING CLASS_NAME TYPE C
EXPORTING POINTER TYPE REF TO OBJECT.
ENDCLASS.
CLASS C3 IMPLEMENTATION.
METHOD CREATE_OBJECT.
CREATE OBJECT POINTER TYPE (CLASS_NAME).
ENDMETHOD.
ENDCLASS.
DATA: R1 TYPE REF TO I1,
R2 TYPE REF TO C2,
R3 TYPE REF TO C3,
R TYPE REF TO OBJECT.
START-OF-SELECTION.
CREATE OBJECT R3.
R3->CREATE_OBJECT( EXPORTING CLASS_NAME = 'C1'
IMPORTING POINTER = R ).
CATCH SYSTEM-EXCEPTIONS MOVE_CAST_ERROR = 4.
R1 ?= R.
ENDCATCH.
IF SY-SUBRC EQ 0.
WRITE / 'OK'.
ENDIF.
R3->CREATE_OBJECT( EXPORTING CLASS_NAME = 'C2'
IMPORTING POINTER = R ).
CATCH SYSTEM-EXCEPTIONS MOVE_CAST_ERROR = 4.
R2 ?= R.
ENDCATCH.
IF SY-SUBRC EQ 0.
WRITE / 'OK'.
ENDIF.
In this example, objects of classes C1 and
C2 are created dynamically using the CREATE_OBJECT
method of class C3. References to the objects are passed
to the container R as EXPORTING
parameters and the calling program can determine (using casting) which of its reference variables are compatible with the class thus created.
Catchable Exceptions
CX_SY_CREATE_OBJECT_ERROR
Non-Catchable Exceptions
ALIASES | Declaration of an alias name |
CALL METHOD | Call of a method |
CLASS ... ENDCLASS | Definition of a class |
CLASS-DATA | Declaration of a static attribute |
CLASS-EVENTS | Declaration of a static event |
CLASS-METHODS | Declaration of a static method |
CREATE OBJECT | Creation of an object |
EVENTS | Declaration of an instance event |
INTERFACE ... ENDINTERFACE | Definition of an interface |
INTERFACES | Including an interface |
METHOD ... ENDMETHOD | Definition of a method |
METHODS | Declaration of an Instance method |
PRIVATE SECTION | Start of private visibility section |
PROTECTED SECTION | Start of a protected visibility section |
PUBLIC SECTION | Start of the public visibility section |
RAISE EVENT | Triggering an event |
SET HANDLER | Registering an event |
Handling Objects