Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Static constructor

lakshminarasimhan_n4
Active Contributor
0 Likes
2,872

Hi,

I have a class A and a class constructor in the class A and another class B which inherits class A.

There is a class C. Class C is independent and has a class constructor too. It does not inherit any class.

After start of selction event, i have created 3 data objects type ref to A, B and C.

After that i have created all of the objects using the statement "create object".

when i debugged the program the static constructor of the class A was called

even before type ref to statement. But the static constructors of the class C was

called only when the statement create object C was executed.

Even i commented the statement "Data : aa type ref to A & create object aa", still the class constructor

of class A was getting called!!!!! Can some one explain the above?

Regards,

Lakshminarasimhan.N

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,371

If I understood you correctly class B is a subclass of class A. Class A has a [static constructor|http://help.sap.com/abapdocu_70/en/ABENCONSTRUCTOR.htm] and you commented out usage of class A references.

If you just comment out reference and usage of class A, but still create an object for B or access any static attribute or method of class B, the static constructor of A should be called. Please check out the ABAP online help on [inheritance and constructors|http://help.sap.com/abapdocu_70/en/ABENINHERITANCE_CONSTRUCTORS.htm], which will explain in detail how constructors are invoked.

Cheers, harald

15 REPLIES 15
Read only

uwe_schieferstein
Active Contributor
0 Likes
2,371

Hello Lakshminarasimhan

The fact that that the CLASS_CONSTRUCTOR method of class "A" is triggered even though you removed the references to this class from your report implies that SOMEWHERE else class "A" is touched and therefore its static constructor executed.

Regards

Uwe

Read only

Former Member
0 Likes
2,372

If I understood you correctly class B is a subclass of class A. Class A has a [static constructor|http://help.sap.com/abapdocu_70/en/ABENCONSTRUCTOR.htm] and you commented out usage of class A references.

If you just comment out reference and usage of class A, but still create an object for B or access any static attribute or method of class B, the static constructor of A should be called. Please check out the ABAP online help on [inheritance and constructors|http://help.sap.com/abapdocu_70/en/ABENINHERITANCE_CONSTRUCTORS.htm], which will explain in detail how constructors are invoked.

Cheers, harald

Read only

0 Likes
2,371

Hello Lakshminarasimhan

I guess Harald's explanation will be correct whereas the remarks by Suhas are simply wrong.

Regards

Uwe

Read only

0 Likes
2,371

There is one simple rule which says when the class_constructor is called.

Class constructor is called when the ABAP environment accesses the class for the first time (not on definitions of reference variables but before execution of some method or before execution of instance constructor).

Second helfpful rule is that class constructors are called in a row in an iheritance tree, from the most general class to the most specialised.

Example:

REPORT z_test.
CLASS lcl_a DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS class_constructor.
ENDCLASS.                    "lcl_a DEFINITION
CLASS lcl_b DEFINITION INHERITING FROM lcl_a.
  PUBLIC SECTION.
    CLASS-METHODS class_constructor.
ENDCLASS.                    "lcl_b DEFINITION
CLASS lcl_c DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS class_constructor.
ENDCLASS.                    "lcl_c DEFINITION
CLASS lcl_a IMPLEMENTATION.
  METHOD class_constructor.
    WRITE / 'CC > A'.
  ENDMETHOD.
ENDCLASS.                    "lcl_a IMPLEMENTATION
CLASS lcl_b IMPLEMENTATION.
  METHOD class_constructor.
    WRITE / 'CC > B'.
  ENDMETHOD.
ENDCLASS.                    "lcl_b IMPLEMENTATION
CLASS lcl_c IMPLEMENTATION.
  METHOD class_constructor.
    WRITE / 'CC > C'.
  ENDMETHOD.
ENDCLASS.                    "lcl_c IMPLEMENTATION
DATA: g_rcl_a TYPE REF TO lcl_a,
           g_rcl_b TYPE REF TO lcl_b,
           g_rcl_c TYPE REF TO lcl_c.
START-OF-SELECTION.
  WRITE / 'Create A'.
  CREATE OBJECT g_rcl_a.
  WRITE / 'Create B'.
  CREATE OBJECT g_rcl_b.
  WRITE / 'Create C'.
  CREATE OBJECT g_rcl_c.

The output is:

Create A

CC > A "rule 1 - create object accesses for the first time

Create B

CC > B "rule 1 - create object accesses for the first time

Create C

CC > C "rule 1 - create object accesses for the first time

But if you comment out lines:

WRITE / 'Create A'.

CREATE OBJECT g_rcl_a.

the output will be:

Create B

CC > A "rule 2 - call in a row class constructors and certainly rule 1 also applies

CC > B

Create C

CC > C

I hope it solved your question.

Regards,

Edited by: Krzysztof Usowicz on Sep 15, 2010 11:16 AM

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
2,371

Hello Uwe,

I'm newbie in OOPs ABAP. Can you please explain what's wrong in my interpretation of CLASS_CONSTRUCTOR v/s CONSTRUCTOR ?

Actually i was trying to explain this behavior :

.. when i debugged the program the static constructor of the class A was called

even before type ref to statement. But the static constructors of the class C was

called only when the statement create object C was executed ..

BR,

Suhas

Read only

0 Likes
2,371

Hello Suhas

The static constructor is executed when the class is "touched", i.e. one of its methods called or just a read access to a static variable. At that time there is neither an iinstance around nor needed.

The constructor method is executed every time you are using the statement


CREATE OBJECT go_class ...

If the class has no explicit CONSTRUCTOR method then the implicit CONSTRUCTOR method will be called.

Regards

Uwe

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
2,371

Hello Uwe,

I think that's exactly what i tried to convey in my original post

Anyways thanks for your explanation though. But i'm a lil' bit confused about this statement:

If the class has no explicit CONSTRUCTOR method then the implicit CONSTRUCTOR method will be called.

May be i missed this portion while going through Dr. Horst Keller's Book. My bad !!

BR,

Suhas

Read only

0 Likes
2,371

Hello Suhas

The implicit constructor is also called the default constructor (in Java and presumably any other OO-language).

Simply speaking the implicit constructor is like an empty explicit CONSTRUCTOR method.-

Regards

Uwe

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
2,371

Hello,

There is a difference between CONSTRUCTOR & CLASS_CONSTRUCTOR.

CLASS_CONSTRUCTOR are static constructors which are called before the class is instantiated. SAP has provided CLASS_CONSTRUCTOR to ensure that only one instance of the class should be active at a time (in OOPs lingo it's called Singleton principle). Looks like Class A has a CLASS_CONSTRUCTOR defined which explains why the constructor is called before the instantiation !

On the other CONSTRUCTOR are instance methods which are called whenever the class is instantiated. Your class C has a CONSTRUCTOR method defined which gets triggered whenever an object is created from the class.

Hope i'm clear.

BR,

Suhas

Read only

Former Member
0 Likes
2,371

Apart from the misleading statement when the [static class constructor|http://help.sap.com/abapdocu_70/en/ABENCONSTRUCTOR.htm] is called (which Uwe corrected and the previous link contains all the details when the static constructor is called), the following statement is also wrong:

SAP has provided CLASS_CONSTRUCTOR to ensure that only one instance of the class should be active at a time (in OOPs lingo it's called Singleton principle).

The static class constructor is not enforcing (and cannot by itself) the [singleton pattern|http://en.wikipedia.org/wiki/Singleton_pattern] (i.e. only one instance of the class exists). Take as an example classes with static and instance attributes; here you could use the static constructor to initialize the [static attributes|http://help.sap.com/abapdocu_70/en/ABENCLASS_ATTRIBUTES.htm] and the instance constructor to initialize the instance attributes. The constructors itself do not limit the number of object instances that can be created. This is something the developer has to explicitly code in ABAP.

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
2,371

Hello Harald,

Thanks for pointing out the mistakes. My OO-ABAP knowledge is very bookish, still have a lot to explore

Cheers,

Suhas

Read only

Former Member
0 Likes
2,371

Hello Lakshminarasimhan

In your case you have inherited class b so whenever you create object for class b the constructor of super class was called.

eventhough you have commented class a reference and object.

If you comment both class a and class b references and objects then construtor of class a wont be called.

To which class you are creating object that class constructor only will be called generally.

In your case its due to inhertitance............

Read only

lakshminarasimhan_n4
Active Contributor
0 Likes
2,371

Hi Abap Gurus,

Thanks a lot for you all. I got good clarity over the class constructors and instance constructors and how they are getting called.

Regards,

Lakshminarasimhan.N

Read only

0 Likes
2,371

I am pretty late to comment on this post, but you can read more on CONSTRUCTORS and CLASS-CONSTRUCTORS at [CLASS_CONSTRUCTOR and CONSTRUCTOR: Who comes before whom?|http://help-abap.blogspot.com/2011/02/classconstructor-and-constructor-who.html]

Regards,

Naimesh Patel

Read only

0 Likes
2,371

Good read, Naimesh! You just made my day