ABAP Keyword Documentation → ABAP − Reference → Creating Objects and Values → CREATE OBJECT
CREATE OBJECT - TYPE
Other versions: 7.31 | 7.40 | 7.54
Syntax
CREATE OBJECT oref [area_handle]
TYPE { class
[parameter_list] }
| { (name) [
parameter_list|
parameter_tables] }.
Effect
Creates an instance of an explicitly specified class. A
concrete class that is more
specific than or identical to the static type of the object reference variable oref
can be specified. The reference variable oref
can be a class reference variable or an interface reference variable. The name of the class can be specified as follows:
-
class
class
.
-
(name)
name
. The data object
name
must contain the name of the class in uppercase letters when the statement is executed. The class name in name
can also be an
absolute type name. The following can be specified for name
:
- Literal or constant
If the data objectname
is specified as a character literal or as a constant, it can be evaluated statically and the specified class is identified as the used object.
- Variable
If the data objectname
is specified as a variable, it is specified only dynamically and the content is not evaluated statically.
name
is not evaluated until runtime (in both cases).
The class must be usable at the current location:
-
The addition
CREATE
of the class definition must permit objects to be created. -
The package check must permit an object of a global class to be created.
Notes
-
If the class is specified dynamically, the parameters can either be specified statically in
parameter_list
or dynamically inparameter_tables
. -
When a class is used, the instance
operator
NEW
acts like the statementCREATE OBJECT oref TYPE class
and can be used in general expression positions. The content ofname
cannot be specified dynamically here.
Security Note
If used wrongly, creating objects dynamically can present a serious security risk. Names of classes that are passed to a program from the outside must be checked thoroughly before being used in dynamic statements. The system class CL_ABAP_DYN_PRG, for example, can be used to do this. See Dynamic Calls.
Example
Creates an instance of a class cls
using an explicit reference to the class.
The static type of the reference variable is the root class object
and is
therefore more general than any other class. Components must be accessed dynamically or a cast must be performed (as shown here).
CLASS cls DEFINITION.
PUBLIC SECTION.
DATA attr TYPE string VALUE 'Object'.
ENDCLASS.
DATA oref TYPE REF TO object.
CREATE OBJECT oref TYPE cls.
cl_demo_output=>display( CAST cls( oref ) ).