Tuesday, March 26, 2013

Static Constructor is Called Once in Program


Static constructor of a class is called only one time in a program. In this matter when a sub class is accessed then its static constructor is called only once in that program. Here it needs to be remembered that whenever a static constructor is executed the system checks any of its super class constructor has been executed or not. Now if the super class constructor has not been executed then the system will execute that first then the sub class constructor will be executed.

Here we have a program where we have defined a grand parent class with a static constructor method. We have implemented it with a specific functionality. Next we have created a parent class with having the same static constructor but we have implemented it differently. Similarly we have created a child class with having the same static constructor but we have implemented it more differently.
The naming convention will be class_constructor. 

Now in the start of selection we have instantiated the child class only. In the output we can get both the functions of grandparent class and parent class and also child class static constructor methods. The output will come with this order: Grand Parent->Parent->Child. Here the object of parent and child class need not to be created to view the output.

We have mentioned the previous program here and the constructor is class_constructor. There is no need to call method separately for static constructor. It is called automatically by instantiating.


REPORT  zsr_test NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------*
*       CLASS cl_grand DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_grand DEFINITION.
  PUBLIC SECTION.
    CLASS-DATA v_test TYPE char40.
    CLASS-METHODS class_constructor.
ENDCLASS.                    "cl_grand DEFINITION

*----------------------------------------------------------------------*
*       CLASS cl_grand IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_grand IMPLEMENTATION.
  METHOD class_constructor.
    v_test = 'Static Constructor - Grand Parent'.
    WRITE: /3 v_test.
  ENDMETHOD.                    "class_constructor
ENDCLASS.                    "cl_grand IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS cl_parent DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_parent DEFINITION INHERITING FROM cl_grand.
  PUBLIC SECTION.
    CLASS-METHODS class_constructor.
ENDCLASS.                    "cl_parent DEFINITION

*----------------------------------------------------------------------*
*       CLASS cl_parent IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_parent IMPLEMENTATION.
  METHOD class_constructor.
    v_test = 'Static Constructor - Parent'.
    WRITE: /3 v_test.
  ENDMETHOD.                    "class_constructor
ENDCLASS.                    "cl_parent IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS cl_child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_child DEFINITION INHERITING FROM cl_parent.
  PUBLIC SECTION.
    CLASS-METHODS class_constructor.
ENDCLASS.                    "cl_child DEFINITION

*----------------------------------------------------------------------*
*       CLASS cl_child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_child IMPLEMENTATION.
  METHOD class_constructor.
    v_test = 'Static Constructor - Child'.
    WRITE: /3 v_test.
  ENDMETHOD.                    "class_constructor
ENDCLASS.                    "cl_child IMPLEMENTATION

START-OF-SELECTION.
  DATA obj_child TYPE REF TO cl_child.
  CREATE OBJECT obj_child.

Below is the Output:

2 comments:

Anonymous said...

SAP Success Factors Real Time Hands on Training in Chennai...

Don't always Depend on Training Institute Alone and so please aware of Best Trainers too..

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

If You need a Best Trainer over SAP Success Factors Means??? Please ready for an DEMO From the Trainer MR.Karthick
CONTACT:8122241286

Both Classroom/Online Training is Available!!!!!!

Business World said...

Thanks for sharing valuable information through your post. The information provided is of great use.
SAP ABAP TRAINING IN HYDERABAD