Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Creating Objects 

CREATE OBJECT

Short Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax Forms


Defining a Class Implicitly

1. CREATE OBJECT oref [area_handle] [parameter_list].

Defining a Class Explicitly

2. CREATE OBJECT oref [area_handle]
                     TYPE { class  [parameter_list] }
                        | { (name) [parameter_list| parameter_tables] }.

Effect

The CREATE OBJECT statement generates an instance of a class or object and assigns the object reference to the oref reference variables. The instance constructor of the class is called directly after the object is created.

By default, the object is created in the internal session (heap) of the current program and remains there for as long as it is required. If no heap references point to the object, which means that

  • no object reference points to the object,
  • no data reference can no field symbol points to an instance attribute or to a part of an instance attribute,

and if no method of the object is registered as an event handler, it is deleted by Garbage Collector. The area_handle addition can be used to create the object as a shared object.

The reference variable oref must be declared as an object reference variable. Instance components of an object created using CREATE OBJECT can only be accessed using object reference variables (see Data Objects in Operand Positions).

The TYPE addition can be used to specify the class of the new object. The static type of the object reference variables must be more general than or identical to the class of the object generated (in accordance with the rules for Assignments Between Object Reference Variables).

If the additions parameter_list and parameter_tables are used, the non-optional input parameters of the first explicitly implemented instance constructor must be filled that are located in the path of the inheritance tree between the instantiated class and the root class object. These additions can also be used to assign return values to the non-class-based exceptions of the instance constructor.

If handleable exception is raised in the runtime environment when the object is generated, the object is not generated and the object reference variable oref is initialized. If a handleable exception is raised when the object is created in the instance constructor of the class, the object is deleted and the object reference variable oref is initialized.

Return Value


Notes

  • An instance of a class can be created only where permitted by the CREATE addition of the CLASS DEFINITION statement.
  • If the reference variable oref specified after CREATE OBJECT is simultaneously passed to the instance constructor, then when this variable is executed, it points to the newly generated object. To pass a reference to an existing object to the instance constructor, a different reference variable must be used.
  • The statement CREATE OBJECT creates a heap reference. All references that point to the object or its parts are also heap references and keep the object alive. The same applies to field symbols that point to instance attributes or to their parts.

Exceptions


Catchable Exceptions

CX_SY_CREATE_OBJECT_ERROR

  • Cause: An attempt was made to instantiate an abstract class.
    Runtime Error: CREATE_OBJECT_CLASS_ABSTRACT
  • Cause: The class specified in the TYPE addition does not exist.
    Runtime Error: CREATE_OBJECT_CLASS_NOT_FOUND
  • Cause: An attempt was made to instantiate a private class externally.
    Runtime Error: CREATE_OBJECT_CREATE_PRIVATE
  • Cause: An attempt was made to instantiate a protected class externally.
    Runtime Error: CREATE_OBJECT_CREATE_PROTECTED


Non-Catchable Exceptions

  • Cause: A reference must be specified as the target variable.
    Runtime Error: CREATE_OBJECT_NO_REFTYPE:

Continue

CREATE OBJECT oref

CREATE OBJECT - TYPE

CREATE OBJECT - AREA HANDLE

CREATE OBJECT - parameter_list

CREATE OBJECT - parameter_tables