‎2010 Aug 29 4:23 PM
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
‎2010 Aug 30 7:03 AM
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
‎2010 Aug 29 9:20 PM
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
‎2010 Aug 30 7:03 AM
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
‎2010 Aug 30 8:48 AM
Hello Lakshminarasimhan
I guess Harald's explanation will be correct whereas the remarks by Suhas are simply wrong.
Regards
Uwe
‎2010 Aug 30 10:46 AM
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
‎2010 Aug 30 1:49 PM
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
‎2010 Aug 30 5:13 PM
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
‎2010 Aug 30 5:55 PM
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
‎2010 Aug 31 7:40 AM
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
‎2010 Aug 30 7:42 AM
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
‎2010 Aug 31 7:09 AM
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.
‎2010 Aug 31 11:28 AM
Hello Harald,
Thanks for pointing out the mistakes. My OO-ABAP knowledge is very bookish, still have a lot to explore
Cheers,
Suhas
‎2010 Aug 30 1:37 PM
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............
‎2010 Sep 09 12:09 PM
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
‎2011 Feb 21 4:56 PM
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
‎2011 Feb 22 6:56 AM