ABAPCREATE_OBJECT_ABAP_OBJECTS - CREATE OBJECT ABAP OBJECTS
General Material Data PERFORM Short ReferenceThis documentation is copyright by SAP AG.
CREATE - Create an Object in ABAP Objects
Syntax
CREATE OBJECT ... .
Variants:
1. CREATE OBJECT cref.
2. CREATE OBJECT ref TYPE class.
3. CREATE OBJECT ref TYPE (class).
Effect
Creates an object, that is, the instance of aclass, inABAP Objects. The additionsCREATE PUBLIC|PROTECTED|PRIVATE of the statement CLASS determine who is allowed to create objects of a class.
Variant 1
CREATE OBJECT cref.
Additions
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. Referencevariables are typed using the TYPE REF TO addition of the DATAor TYPES statements. You cannot create reference variables with reference to aninterface 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 usedto 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 MOVEstatement), 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 OBJECTstatement is executed. If there is already a reference to an object in cref, the reference tothe 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.
Note
If a class contains an instanceconstructor (the special method CONSTRUCTOR), the CREATE OBJECT statement calls this method after the object is generated completely. If a class contains astatic 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 alsoVisibility of Instance Constructors.
Example
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 referenceto 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).
Addition 1
... EXPORTING p1 = f1 ... pn = fn
Effect
If the EXPORTING addition is used, appropriately typed actual parameters f1...fn must be passed to all IMPORTING parameters p1 ... pn of theinstance constructor of the class, provided they have not been declared optional.
Example
See the example in Addition 2, EXCEPTIONS.
Addition 2
... EXCEPTIONS except1 = rc1 ... exceptn = rcn
Effect
You can use the optional EXCEPTIONS addition to handle exceptions in theinstance constructorof 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 linkedto the return code rc. If an exception is triggered, the system stops executing the CONSTRUCTORmethod, 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 MESSAGERAISING is not handled, the message specified is sent and the system proceeds according to the message type.
Example
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 EXCEPTIONSparameters. 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.
Variant 2
CREATE OBJECT ref TYPE class.
Additions
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 classthat is explicitly declared as a class. ref can be any reference variable that can pointto the class declared. You can also use class references that are typed with reference to the classclass or its superclasses, and interface references that are typed with reference to an interface that class has implemented.
After this statement has been executed, the instance exists in the context of the current internalmode and the reference refpoints to this instance. Class references typed for superclasses, andinterface references can access statically only those components known to them. This variant of theCREATE OBJECT statement saves you from creating a class reference cref, which is only needed for creating instances.
Otherwise this variant is very similar to the first variant. In particular, you must use the EXPORTING addition to pass appropriately typed actual parameters f1...fn to all IMPORTING parameters of theinstance constructorof the class class, provided they have not been declared optional. Exceptions can be handled with the optional EXCEPTIONS addition.
Addition 1
... EXPORTING p1 = f1 ... pn = fn
Effect
As variant 1.
Addition 2
... EXCEPTIONS except1 = rc1 ... exceptn = rcn
Effect
As variant 1.
Example
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.
Variant 3
CREATE OBJECT ref TYPE (class).
Additions
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 fieldclass. At runtime, the class name from the field class is inserted in the statement andhandled as if it were static. In particular, local classes obscure identically-named global classes in the dynamic variant as well.
ref can be any reference variable. The system checks at runtime whether or not the type of reference variable is compatible with the class declared. If not, a runtime error occurs.
Note
If the class is declared dynamically, you can also specify absolute type names.
Addition 1
... EXPORTING p1 = f1 ... pn = fn
Addition 2
... EXCEPTIONS except1 = rc1 ... exceptn = rcn
Addition 3
... PARAMETER-TABLE itab
Addition 4
... EXCEPTION-TABLE itab
Effect
As with the above variants and with the dynamicmethod call, these additions fill the IMPORTING parameters of the instance constructor statically or dynamically and handle the classic exceptions.
Example
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_OBJECTmethod of class C3. References to the objects are passed to the container R as EXPORTINGparameters and the calling program can determine (using casting) which of its reference variables are compatible with the class thus created.
Note
If executed successfully, the CREATE OBJECTstatement sets SY-SUBRC to zero. Values of SY-SUBRC other than zero can occur depending on the handling of exceptions by the instance constructor.
Exceptions
Catchable Exceptions
- Cause: System tried to instantiate an abstract class.
Runtime Error: CREATE_OBJECT_CLASS_ABSTRACT (catchable) - Cause: Cannot find the class declared in the TYPE addition.
Runtime Error: CREATE_OBJECT_CLASS_NOT_FOUND (catchable) - Cause: System tried to instantiate a private class from outside the class itself.
Runtime Error: CREATE_OBJECT_CREATE_PRIVATE (catchable) - Cause: System tried to instantiate a protected class from outside the class itself.
Runtime Error: CREATE_OBJECT_CREATE_PROTECTED (catchable)
Non-Catchable Exceptions
- Cause: You must declare a reference as a target variable.
Runtime Error: CREATE_OBJECT_NO_REFTYPE
Related
| 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 |
Additional help
Fill RESBD Structure from EBP Component Structure ROGBILLS - Synchronize billing plans
This documentation is copyright by SAP AG.
Length: 23541 Date: 20120522 Time: 042631 triton ( 561 ms )






