SAP CLASS-METHODS CONSTRUCTOR ABAP Statements
Get Example source ABAP code based on a different SAP table
CLASS-METHODS -
• class_constructor CLASS-METHODS
ABAP Syntax
What does it do? This statement declares the static constructor
Each class has a predefined method
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.
Latest 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 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.
In static constructors, you cannot declare class-based exceptions using
If a
An unsuccessful dynamic access to a nonexistent component of a class does not count as an access to the class and the static constructor is not executed in this case.
Example ABAP Coding
When a class is first accessed, the static constructor of this class uses the system field
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.
Return to menu