Ansicht
Dokumentation

ABAPCREATE_OBJECT_ABAP_OBJECTS - CREATE OBJECT ABAP OBJECTS

ABAPCREATE_OBJECT_ABAP_OBJECTS - CREATE OBJECT ABAP OBJECTS

Fill RESBD Structure from EBP Component Structure   General Material Data  
This documentation is copyright by SAP AG.
SAP E-Book

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 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.

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. 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.

Note

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.

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 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).

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 the instance 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 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.

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 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.

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 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.


After this statement has been executed, the instance exists in the context of the current internal mode and the reference refpoints to this instance. Class references typed for superclasses, and interface references can access statically only those components known to them. This variant of the CREATE 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 the instance constructor of 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 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.


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 dynamic method 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_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.

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

CX_SY_CREATE_OBJECT_ERROR

  • 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

Handling Objects






TXBHW - Original Tax Base Amount in Local Currency   CPI1466 during Backup  
This documentation is copyright by SAP AG.

Length: 23060 Date: 20240328 Time: 130904     sap01-206 ( 284 ms )