Constructors

Constructors are special methods that produce a defined initial state of objects and classes. The state of an object is determined by its instance attributes and static attributes. 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.

For inheritance some special rules apply to constructors that are not described here but in the inheritance context.

Instance Constructors

Each class has one instance constructor. It is a predefined instance method of the CONSTRUCTOR class. If you want to use the instance constructor, the CONSTRUCTOR method must be declared in the public section of the class using the METHODS statement, and implemented in the implementation section. Unless it is explicitly declared, the instance constructor is an empty method.

The declaration in the public area is necessary for technical reasons. The actual visibility of the instance constructor is controlled by the CREATEPUBLIC|PROTECTED|PRIVATE additions to the CLASS statement. For more information refer to Visibility of Instance Constructors.

Instance constructors are executed once for each instance. They are called automatically, immediatly after you have created an instance using the CREATE OBJECT statement. It is not possible to call an instance constructor directly using the CREATE OBJECT statement.

An instance constructor can contain an interface with IMPORTING parameters 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 transfer parameters and handle exceptions, use the EXPORTING and EXCEPTIONS additions to the CREATE OBJECT statement (see the example in the documentation for CREATE OBJECT).

Static Constructors

Each class has a single static constructor. This is a predefined, public, static method of the CLASS_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 CLASS-METHODS statement, and implement it in the implementation part. The static constructor has no interface parameters and cannot trigger exceptions. Unless you implement it explicitly it is merely an empty method.

The static constructor is executed once in each program. It is called automatically for the class class before the class is accessed for the first time - that is, before one of the following actions:

The static constructor is always called immediately before the action is executed, 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 the access occurs.

Caution: The static constructor must not access its own class. Otherwise a runtime error will occur.

Caution: The point at which the static constructor is executed has not yet been finalized, and it may yet be changed. SAP only guarantees that it will be executed before the class is accessed for the first time. You are recommended not to write programs that require the static constructor to be executed at the beginning of a processing block.