ABAP - Keyword Documentation →  ABAP - Reference →  Declarations →  Declaration Statements →  Classes and Interfaces →  Components in Classes and Interfaces →  Methods →  CLASS-METHODS → 

CLASS-METHODS - class_constructor

Quick Reference

Syntax

CLASS-METHODS class_constructor.

Effect

This statement declares the static constructor class_constructor of a class. It can only be used in the public visibility section of the declaration part 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 called automatically exactly once per class and internal session before the class is first accessed. The class is accessed when an instance of the class is created or a static component is addressed using the class component selector. The exception here is addressing a type or a constant of the class.

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

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.