Tuesday, June 23, 2009

CLASS-METHODS - class_constructor

Syntax

CLASS-METHODS class_constructor.

Effect

This statement declares the static constructor class_constructor of the class. It can only be used in the public visibility section of the declaration section of a class.

Each class has a predefined method class_constructor in its public visibility section. Its functions can be implemented class-specifically by explicit declaration. Without explicit declaration, the static constructor is empty.

The static constructor is automatically called exactly once per class and internal session before the class is first accessed. An access to the class is the creation of an instance of the class or the addressing of a static component using the class component selector.

When a subclass is first accessed, the inheritance tree is searched for the next-highest superclass whose static constructor was not yet called. Then the static constructor of this superclass is executed followed by those of all subsequent subclasses up to the subclass in question. The static constructor must be fully executed, otherwise a runtime error occurs.

Like all static methods, the static constructor can only access the static components of its class. Furthermore, the static constructor cannot explicitly address its own class.

Notes

  • Like instance constructors, static constructors are also an exception to the rule that all public components along a path in the inheritance tree are in one namespace.

  • The execution time of the static constructor is not fixed. Only its call before the first access to the class is guaranteed. It can therefore happen that static methods are executed before the static constructor is ended.

  • The execution sequence of static constructors is dependent on the program flow. Statc constructors must be implemented so that they can be executed in any sequence.

  • In static constructors, you cannot declare class-based exceptions using RAISING, since it is generally not specified whether the user of a class is the first user and whether or not he must handle exceptions propagated by the static constructor.

  • If a statc component of a superclass is addressed using the name of a subclass, then the superclass is addressed and its static constructor is executed, but not the static constructor of the subclass.


Example

When a class is first accessed, the static constructor of this class uses the system field sy-repid to set the static attribute access_program<> for the name of the program of an internal session that uses the class first.

CLASS some_class DEFINITION.
PUBLIC SECTION.
CLASS-METHODS class_constructor.
PRIVATE SECTION.
CLASS-DATA access_program TYPE sy-repid.
ENDCLASS.

CLASS some_class IMPLEMENTATION.
METHOD class_constructor.
access_program = sy-repid.
ENDMETHOD.
ENDCLASS.



No comments:

Blog Archive