SAP Tech Pro

SAP Technical Blog
Topprice Price Comparison

Object Oriented Programming in ABAP - Static constructor of a class is called only once per program.

Object Oriented Programming in ABAP - Static constructor of a class is called only once per program.

The first when a  subclass in a program is accessed, its static constructor is executed. But, before it can be executed, the static constructors of all of its super classes must already have been executed. A static constructor may only be called once per program. Therefore, when one first address a subclass, the system looks for the next-highest superclass whose static constructor has not yet been executed. It executes the static constructor of that class, followed by those of all classes between that class and the subclass that is addressed.

This program contains three classes:-

  • FATHER with its own static constructor method
  • SON, subclass of FATHER with its own static constructor method.

An object is created finally from the class SON, which triggers the constructor methods in the order: FATHER->SON.

Now, an object is created from the class FATHER. But, that did not trigger constructor of class FATHER, because that had already been triggered by the program when an object was created from the class SON.

This establishes the theme.



REPORT zstp_oo_abap.

CLASS  father DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS : class_constructor.
ENDCLASS.

CLASS  father IMPLEMENTATION.
  METHOD class_constructor .
    WRITE:/5 'I am father'.
    SKIP.
  ENDMETHOD.
ENDCLASS.

CLASS  son DEFINITION INHERITING FROM father.
  PUBLIC SECTION.
    CLASS-METHODS : class_constructor.
ENDCLASS.

CLASS  son IMPLEMENTATION.
  METHOD class_constructor .
    WRITE:/5 'I am son'.
    SKIP.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.

  DATA: myson TYPE REF TO son.
  CREATE OBJECT: myson.

  DATA : myfather TYPE REF TO father.
  CREATE OBJECT : myfather.

Output -

Static constructor of a class is called only once per program

Add comment

Before Buying, Compare Prices at Topprice.in

Topprice.in