CREATE OBJECT

CREATE Syntax Diagram

Variants:
1. CREATE OBJECT obj.
2. CREATE OBJECT obj class.

Effect

Generates an object (an instance of a class) in
ABAP Objects, the object-oriented extension of

ABAP

.

The CREATE OBJECT statement creates an instance of a class, which is defined using the CLASS - ENDCLASS statement block and which is the reference type of an object variable obj. You must therefore declare an object variable obj using the addition REF TO, TYPES or DATA before you can create an object.

You cannot address an object directly in a program. Instead, you always use reference variables. Once you have created an object, the object variable obj points to the object. As the program runs, other object variables with the same type as obj can also address the object. Equally, all interface variables pointing to interfaces that are implemented by the class to which obj relates can also address the object. The original object variable obj does not always have to point to the same object, since you can reassign references between objects of the same type. An object exists while there is at least one reference variable pointing to it. When no more reference variables point to it, it is deleted by the automatic memory management for objects.

Reference variables can access the visible (instance and class) attribtues and (instances and class) methods of the object. While object variables can address all visible components, interface variables can only access the components that are defined in the interface.
To address objects (instances) using reference variables, use the following syntax:

obj->attr Accesses an
(instance or class ) attribute attr.

CALL obj->meth Calls an
(Instance or class method meth.

obj->ifac~attr Accesses an attribute attr of the
implemented interface ifac.

CALL obj->ifac~meth Calls a method meth of the
implemented interface ifac.

Example

CLASS C_TEST DEFINITION. 
 PUBLIC SECTION. 
   DATA ATTR. 
ENDCLASS. 
 
TYPES: T_C_TEST TYPE REF TO C_TEST. 
 
DATA: O_TEST_1 TYPE T_C_TEST, 
	O_TEST_2 LIKE O_TEST_1. 
 
CREATE OBJECT: O_TEST_1, 
			 O_TEST_2. 
 
O_TEST_1->ATTR = 'X'. 
O_TEST_2->ATTR = 'Y'. 
... 
O_TEST_1 = O_TEST_2. 
... 
 
CLASS C_TEST IMPLEMENTATION. 
  ... 
ENDCLASS.

The DATA statement creates two object variables, O_TEST_1 and O_TEST_2, with reference to the type of class C_TEST. CREATE OBJECT declares two separate instances (objects) of the class C_TEST, each of which may be addressed using both object variables. After the statement O_TEST_1 = O_TEST_2, O_TEST_1 also addresses the second object. Since there are no more references pointing to the first object, the automatic 'garbage collector' in the system deletes it.

Addition:
... LANGUAGE lg

To address an OLE automation server, such as EXCEL from ABAP

, you must register it in the SAP System. You can assign an automation server to a class using Transaction SOLE.
The CREATE statement creates the initial object of the class, which other statements can then work with.
The return code SY-SUBRC indicates whether the system was able to create the object:

SY-SUBRC = 2:

Error calling a function in SAPgui. The OLE function modules are only implemented under Windows.

SY-SUBRC = 3:

The OLE-API call returned an error (possibly memory problems).

Note

The system checks authorization automatically. If the authorization check fails, a runtime error occurs. You can check authorization in advance using the AUTHORITY_CHECK_OLE function module.

Effect

The language ID determines the language used for the method name and attributes of the object class.
The default language is English.

CREATE OBJECT

INCLUDE OLE2INCL. 
DATA EXCEL TYPE OLE2_OBJECT. 
CREATE OBJECT EXCEL 'Excel.Application'. 

Related

SET PROPERTY
GET PROPERTY
CALL METHOD
FREE OBJECT