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
You can assign contents to attributes using the
VALUE addition in the
DATA statement. Constructors are necessary when you want to set the initial state of an object dynamically.
Like normal methods, there are two types of constructor: instance constructors and static constructors.
Special rules apply to constructors during inheritance. These rules are not described in this document, but can be found here.
Each class has one instance constructor. This is a predefined instance method of the
class. If you want to use the instance constructor, the
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
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 you have created an instance using the CREATE
OBJECT statement. It is not possible to call an instance constructor directly using the
CALL METHOD statement.
An instance constructor can contain an interface with
and exceptions. You define the interface using the same syntax as for normal methods in the
METHODS statement. 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
additions of the
CREATE OBJECT statement.
Each class has a single static constructor. This is a predefined public
static method of the
constructor class. If you want to use the static constructor, you must declare
the static method
class_constructor in the public section of the declaration
part of the class using the
statement, and implement it in the implementation part. The static constructor has no interface parameters and cannot raise exceptions. Unless you implement it explicitly, it 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 your first access to the class is 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.
- 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 sequence of static constructors is dependent on the program flow. Static constructors must be implemented so that they can be executed in any sequence.