Google Search - Blog...........

OOPS: 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 superclasses 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.
-------------------------------------------------------------------------------------------
REPORT YSUBOOPS18.

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.
Write:/5 ‘Hi’.
data : myfather type ref to father.
create object : myfather.


Output:

I am father
I am son
Hi

No comments:

Post a Comment