Mentor SAP
2018-02-02 Submitted by:- Admin

Static constructor is declared by CLASS-METHODS CLASS_CONSTRUCTOR statement. Here the naming convention must be followed. If we give any other name then that will not be the static constructor. Now static constuctor is called 
1. before any other attributes and methods by using obj->var & obj->meth,
2. before any other static attributes and methods by using cls=>var & cls=>meth,
3. before creating of an object by using create object obj,
4. before registering an event handler method by using set handler cls=>meth for the object obj.

Below is a program where we have defined a static data and a static constructor. The static constructor is implemented in the method - endmethod portion. In the start of selection we write the static attribute but in the output we can see the static constructor function comes first.

REPORT  zsr_test.

*----------------------------------------------------------------------*
*       CLASS cl_construct DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_construct DEFINITION.
  PUBLIC SECTION.
    CLASS-DATA cl_v_text TYPE char40 VALUE 'SAP ABAP Object Oriented'.
    CLASS-METHODS class_constructor.
ENDCLASS.                    "cl_construct DEFINITION

*----------------------------------------------------------------------*
*       CLASS cl_construct IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_construct IMPLEMENTATION.
  METHOD class_constructor.
    WRITE: / 'Class Constructor gets triggered'.
  ENDMETHOD.                    "class_constructor
ENDCLASS.                    "cl_construct IMPLEMENTATION

START-OF-SELECTION.

  WRITE: / cl_construct=>cl_v_text.

The following is the output.