SAP Tech Pro

SAP Technical Blog
Topprice Price Comparison

OO ABAP - Use of static constructor

OO ABAP - Use of static constructor

Static/class constructors get triggered before any of the following events:-

These two programs will show you that a class constructor gets fired before any of its static components are accessed, or an object is created from the class.


CLASS c1 DEFINITION .
  PUBLIC SECTION.
    CLASS-DATA : num TYPE i VALUE 5.
    CLASS-METHODS :class_constructor.
ENDCLASS.

CLASS c1 IMPLEMENTATION.
  METHOD class_constructor.
    WRITE:/5 'I am class constructor'.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  WRITE:/ c1=>num.

Output –>

image


CLASS c1 DEFINITION .
  PUBLIC SECTION.
    CLASS-DATA : num TYPE i VALUE 5.
    CLASS-METHODS :class_constructor.
ENDCLASS.

CLASS c1 IMPLEMENTATION.
  METHOD class_constructor.
    WRITE:/5 'I am class constructor'.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  DATA : oref TYPE REF TO c1.
  CREATE OBJECT oref.

Output –>

image

Add comment

Before Buying, Compare Prices at Topprice.in

Topprice.in