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.
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.
*----------------------------------------------------------------------*
* 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.
*----------------------------------------------------------------------*
* 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 same program where we have created an object obj. Now we can see the output is coming with only the static constructor data. Here we are not calling the static data so the output only contains static constructor componets. Hence it is seen that static constructor triggers before creating any object.
*&---------------------------------------------------------------------*
*& Report ZSR_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zsr_test.
*----------------------------------------------------------------------*
* CLASS cls DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls DEFINITION.
PUBLIC SECTION.
CLASS-DATA v_txt TYPE char20 VALUE 'Class Constructor'.
CLASS-METHODS class_constructor.
ENDCLASS. "cls DEFINITION
*----------------------------------------------------------------------*
* CLASS cls IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls IMPLEMENTATION.
METHOD class_constructor.
WRITE: / 'Today''s Date:', sy-datum DD/MM/YYYY.
ENDMETHOD. "class_constructor
ENDCLASS. "cls IMPLEMENTATION
START-OF-SELECTION.
DATA obj TYPE REF TO cls.
CREATE OBJECT obj.
The following is the output:
Below is another version of the program where we can see that the static constructor is always triggered first in the processing block like Start-of-selection. We have mentioned the class attribute first and then a simple text in the Start-of-selection. But in the output the static constructor data comes first then rest of the things are coming.
*&---------------------------------------------------------------------*
*& Report ZSR_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zsr_test.
*----------------------------------------------------------------------*
* CLASS cls DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls DEFINITION.
PUBLIC SECTION.
CLASS-DATA v_txt TYPE char10 VALUE 'SAP ABAP'.
CLASS-METHODS class_constructor.
ENDCLASS. "cls DEFINITION
*----------------------------------------------------------------------*
* CLASS cls IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls IMPLEMENTATION.
METHOD class_constructor.
WRITE: / 'Today''s Date:', sy-datum DD/MM/YYYY.
ENDMETHOD. "class_constructor
ENDCLASS. "cls IMPLEMENTATION
START-OF-SELECTION.
WRITE: / cls=>v_txt,
'Object Oriented Concept'.
The following is the output:
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:
Post a Comment