banner ads

Static Constructor.

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.


In similar way if we write any statement after the start of selection, the static constructor will trigger first and then the system will go further, whatever the sequence is.

REPORT  zsr_test.

*----------------------------------------------------------------------*
*       CLASS cl_construct DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_construct DEFINITION.
  PUBLIC SECTION.
    CLASS-DATA    cl_v TYPE char40 VALUE 'I am Class data'.
    CLASS-METHODS class_constructor.
ENDCLASS.                    "cl_construct DEFINITION

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

START-OF-SELECTION.  WRITE: / 'Hello',
         / cl_construct=>cl_v.

In the following program we are comparing class constructor with class method. The class constructor gets triggered first and then it comes for class method.


REPORT  zsr_test.

*----------------------------------------------------------------------*
*       CLASS cl_construct DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_construct DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS: class_meth, class_constructor.
ENDCLASS.                    "cl_construct DEFINITION

*----------------------------------------------------------------------*
*       CLASS cl_construct IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_construct IMPLEMENTATION.
  METHOD class_meth.
    WRITE: / 'Class Method gets triggered'.
  ENDMETHOD.                    "class_meth

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

START-OF-SELECTION.  CALL METHOD cl_construct=>class_meth.

Below is another version of the program where we are declaring an Import parameter to the static constructor element. We are instantiating the parameter with a value. But at the time of compilation we are getting syntactical error which says the Static Constructor cannot have any Import or Export parameter. Following is the reflection:



No comments