ABAP Keyword Documentation → ABAP - Reference → Declarations → Declaration Statements → Classes and Interfaces → ABAP Objects - Overview → Classes → Components of Classes
Constructors of Classes
Constructors are special methods that produce a defined initial state for objects and classes. The state of an object is determined by its
instance attributes and
static attributes.
Contents can be assigned to attributes using the addition VALUE
of the statement
DATA
. Constructors are necessary when the initial state of an object is to be defined dynamically.
Like normal methods, there are two types of constructor: instance constructors and static constructors.
Special rules apply to constructors in cases of inheritance. These rules are not described in this document, but can be found here.
Other versions: 7.31 | 7.40 | 7.54
Instance Constructors
Each class has one instance constructor. This is a predefined instance method of the constructor
class. To use the instance constructor, the constructor
method must be declared
in a visibility area of the class using the
METHODS statement, and implemented in the implementation section. In global classes, the
instance constructor can be declared in the public visibility area only, for technical reasons. In local
classes, the visibility area in which the instance constructor can be declared must be more general
or equal to the instantiability defined by the addition
CREATE of the CLASS
statement,
where the most specialized area is recommended. Unless it is explicitly declared, the instance constructor is an implicit method, which inherits and accesses the interface from the instance constructor in the superclass.
Instance constructors are called once for each instance. They are called automatically, immediately
after an instance is created using the statement
CREATE OBJECT. It is not possible to call an instance constructor directly using the constructor( ...
); see super->constructor( ... )
in the inheritance.
An instance constructor can contain an interface with IMPORTING
parameters
and exceptions. The interface is defined using the same syntax as for regular methods in the statement
METHODS
. The fact that there are
no exporting parameters shows that constructors only exist to define the state of an object and have
no other function. To pass parameters and handle classic exceptions, use the EXPORTING
and EXCEPTIONS
additions of the CREATE OBJECT
statement.
Static Constructors
Each class has a single static constructor. This is a predefined public
static method of the
constructor
class. To use the static constructor, the static method
class_constructor must be declared in the public section of the declaration part of the class
using the CLASS-METHODS
statement, and implemented in the implementation part. The static constructor has no interface parameters and cannot raise exceptions. Unless declared explicitly, the static constructor is merely an empty method.
The static constructor is called once per class and
internal session.
The static constructor of a class class
is called automatically before the
class is accessed for the first time. The static constructor is always called immediately before the
class is accessed, with one exception: If the class is accessed for the first time only to address a static attribute, the static constructor is executed at the beginning of the
processing block (dialog module, event block, procedure) in which access takes place.
Notes
- The point at which the static constructor is called has not yet been finalized. We can currently ensure only that it will be called before the class is accessed for the first time. For this reason, static methods may be executed before the static constructor was ended.
- The execution order of static constructors is dependent on the program flow. Static constructors must be implemented so that they can be executed in any order.