Saturday, August 8, 2020

Instance Vs Static Constructor Execution (CLASS 12)

            Instance Vs Static Constructor Execution (CLASS 12):

 Instance constructor are invoked whenever object of class is created whereas static constructor is invoked only once during the entire class execution.

Constructors are basically used for assigning default values ,similar to INITILIZATION event in reports.

 

 *&---------------------------------------------------------------------*
*& Report  ZOOABAP_CL12
*& Instance Vs Static Constructor Execution
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zooabap_cl12.
CLASS zlcl_abc DEFINITION.
  PUBLIC SECTION .
    METHODS constructor "instance constructor
    CLASS-METHODS class_constructor"static constructors
    CLASS-DATA    TYPE i"Static attribute



ENDCLASS.

CLASS zlcl_abc IMPLEMENTATION.
  METHOD constructor .
    WRITE :'inside the instance constructor..........'.
  ENDMETHOD.
  METHOD class_constructor.
  WRITE :'inside the static   constructor..........'.
ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  FORMAT COLOR 3.
  ULINE.

  WRITE 'Using First Object ob1....'.
  DATA ob1 TYPE REF TO zlcl_abc.
  CREATE OBJECT ob1.

  ULINE.
  FORMAT COLOR 7.
  WRITE  'Using the Second Object OB2'.
  DATA ob2 TYPE REF TO zlcl_abc.
  CREATE OBJECT ob2.

  ULINE.

  FORMAT COLOR 4.
  WRITE 'Using the Third Object OB3'.
  DATAob3 TYPE REF TO zlcl_abc.
  CREATE OBJECT ob3.