CLASS_CONSTRUCTOR and CONSTRUCTOR: Who comes before whom?

By | February 17, 2011 | ABAP Objects, OO Concepts | 29,188 | 9

Today we will see how the constructor and class-constructor triggers at runtime.

What is Constructor?

Lets see what is CONSTRUCTOR and CLASS_CONSTRUCTOR

CONSTRUCTOR

Whenever we instantiate the object using the statement CREATE OBJECT statement, CONSTRUCTOR would be called. System creates an object before it calls the CONSTRUCTOR method.

CLASS_CONSTRUCTOR

This type of constructor is the static constructor. This would be automatically accessed when system accesses the class for the first time.

CONSTRUCTOR( ) is called when we instantiate the object, whereas call of CLASS_CONSTRUCTOR( ) method is different based how the class is accessed.

Class Hierarchy UML

Let’s see it in details using this Classes. Each of this class has a CONSTRUCTOR( ), a CLASS-CONSTRUCTOR( ) and a Constant except class LCL_C.

Code Snippet

Code Snippet for class hierarchy setup:

*&---------------------------------------------------------------------*
*& Developer : Naimesh Patel
*& Purpose   : CLASS_CONSTRUCTOR and CONSTRUCTOR explaination
*&---------------------------------------------------------------------*
*
REPORT  ZTEST_NP.
*
* ==== LCL_A ===== *
*
CLASS lcl_a DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODSclass_constructor.
    CONSTANTSc_a TYPE char1 VALUE 'A'.
    METHODSconstructor.
ENDCLASS.                    "lcl_a DEFINITION
*
CLASS lcl_a IMPLEMENTATION.
  METHOD class_constructor.
    WRITE' Class Constructor A'.
  ENDMETHOD.                    "class_constructor
  METHOD constructor.
    WRITE'  Constructor A'.
  ENDMETHOD.                    "constructor
ENDCLASS.                    "lcl_a IMPLEMENTATION
*
* ==== LCL_B ===== *
*
CLASS lcl_b DEFINITION INHERITING FROM lcl_a.
  PUBLIC SECTION.
    CONSTANTSc_b TYPE char1 VALUE 'B'.
    CLASS-METHODSclass_constructor.
    METHODS constructor.
ENDCLASS.                    "lcl_b DEFINITION
*
CLASS lcl_b IMPLEMENTATION.
  METHOD class_constructor.
    WRITE ' Class Constructor B'.
  ENDMETHOD.                    "class_constructor
  METHOD constructor.
    super->constructor).
    WRITE '  Constructor B'.
  ENDMETHOD.                    "constructor
ENDCLASS.                    "lcl_b IMPLEMENTATION
*
* ==== LCL_C ===== *
*
CLASS lcl_c DEFINITION INHERITING FROM lcl_b.
  PUBLIC SECTION.
    CLASS-METHODSclass_constructor.
    METHODS constructor.
ENDCLASS.                    "lcl_b DEFINITION
*
CLASS lcl_c IMPLEMENTATION.
  METHOD class_constructor.
    WRITE ' Class Constructor C'.
  ENDMETHOD.                    "class_constructor
  METHOD constructor.
    super->constructor).
    WRITE '  Constructor C'.
  ENDMETHOD.                    "constructor
ENDCLASS.                    "lcl_b IMPLEMENTATION
*
* ==== LCL_D ===== *
*
CLASS lcl_d DEFINITION INHERITING FROM lcl_c.
  PUBLIC SECTION.
    CONSTANTSc_d TYPE char1 VALUE 'D'.
    CLASS-METHODSclass_constructor.
    METHODS constructor.
ENDCLASS.                    "lcl_b DEFINITION
*
CLASS lcl_d IMPLEMENTATION.
  METHOD class_constructor.
    WRITE ' Class Constructor D'.
  ENDMETHOD.                    "class_constructor
  METHOD constructor.
    super->constructor).
    WRITE '  Constructor D'.
  ENDMETHOD.                    "constructor
ENDCLASS.                    "lcl_b IMPLEMENTATION
*
* ==== LCL_Z ===== *
*
CLASS lcl_z DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODSclass_constructor.
    CONSTANTSc_z TYPE char1 VALUE 'Z'.
    METHODSconstructor.
ENDCLASS.                    "lcl_z DEFINITION
*
CLASS lcl_z IMPLEMENTATION.
  METHOD class_constructor.
    WRITE' Class Constructor Z'.
  ENDMETHOD.                    "class_constructor
  METHOD constructor.
    WRITE'  Constructor Z'.
  ENDMETHOD.                    "constructor
ENDCLASS.                    "lcl_z IMPLEMENTATION

Code Snippet – Constant access Scenario

LOAD-OF-PROGRAM.
  WRITE'... LOAD-OF-PROGRAM ...'.
*
START-OF-SELECTION.
  SKIP 2.
  WRITE'... START-OF-SELECTION ...'.
  WRITE'lcl_d=>c_d  ..... 'lcl_d=>c_d.

Check this different scenarios:
This code accesses the constant C_D of the class LCL_D. It has executed all the CLASS_CONSTRUCTOR( ) of the higher classes in class hierarchy of class D i.e. Class LCL_A, LCL_B, LCL_C and LCL_D in the sequence. It didn’t execute the CLASS_CONSTRUCTOR( ) of the Class LCL_Z because we are not referring to any component of that class.

Code Snippet – Instantiation scenario

LOAD-OF-PROGRAM.
  WRITE'... LOAD-OF-PROGRAM ...'.
*
START-OF-SELECTION.
  SKIP 2.
  WRITE'... START-OF-SELECTION ...'.
  WRITE'Instantiating LO_D'.
  DATAlo_d TYPE REF TO lcl_d.
  CREATE OBJECT lo_d.

Second scenario, in which we instantiate the object reference to LCL_D. You can see that the CLASS_CONSTRUCTOR and CONSTRUCTOR were not called before START-OF-SELECTION. They were actually called when the CREATE OBJECT statement reached. So, instantiating the object has first called the method CLASS_CONSTRUCTOR( ) of the higher classes in the class hierarchy and then all CONSTRUCTOR( ).

Code Snippet – Complex

Which class-constructor calls first when both classes are not related?

LOAD-OF-PROGRAM.
  WRITE'... LOAD-OF-PROGRAM ...'.
*
START-OF-SELECTION.
  SKIP 2.
  WRITE'... START-OF-SELECTION ...'.
  WRITE'Instantiating LO_D'.
  DATAlo_d TYPE REF TO lcl_d.
  CREATE OBJECT lo_d.
*
  SKIP 1.
  WRITE'lcl_z=>c_z  ..... 'lcl_z=>c_z.
  WRITE'lcl_b=>c_b  ..... 'lcl_b=>c_b.

Wow! Too many different execution paths! System first encountered LCL_Z=>C_Z. So, it has called the CLASS_CONSTRUCTOR of the LCL_Z. After that it encountered LCL_B=>C_B. Thus, it calls the CLASS_CONSTRUCTOR of the class hierarchy of the class LCL_B i.e. CLASS_CONSTRUCTOR of classes LCL_A and LCL_B.

Now, when we instantiated the object LO_D, system calls the methods CLASS_CONSTRUCTOR( ) of the remaining classes of the class hierarchies i.e. method CLASS_CONSTRUCTOR( ) of classes LCL_C and LCL_D. It doesn’t call CLASS_CONSTRUCTOR( ) of the classes LCL_A and LCL_B, because they were already called when it has encountered the access to constant LCL_B=>C_B.

You can find more information of constructors on SAP Keyword documentation – Constructors

Check out all ABAP Objects Concepts

You would also like to explore more about other object oriented concepts in ABAP Objects:

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{272 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 272 articles.

Load comments

9 Comments

  • Anonymous

    Nicely explained!!!!

  • srinivas

    It's the best way of explaining static/instance constructor. Helps a lot in getting clear idea of this concept.

  • Shariq

    Wow… You are a true Contributor. Thanks very much.

  • Arun

    Great !! .. Well explained with examples.. Thank you so much..

  • Anoop

    Very Good Explanation. Thanks a lot..:)

  • Vikas

    Thanks a lot…well explained

  • santhosh

    U r amazing! true didcation towards knowledge transfer! :)

  • John Do

    can you please tell me the purpose of a constructor?

  • Vaibhav

    Well explained sir!!!

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.