Application Development 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: 

static Constructor & Instance constructor

govind_parmar
Participant

hi,

i'm new OOABAP, Recently i had made a simple program in which there were 3 classes, such A,B,C.

B and C are subclasses of A i.e. inherited.

What would be the sequence of triggering of Static and Instance Contructor of A,B and C.

1 ACCEPTED SOLUTION

Former Member

HI,

Assume that all the three classes have their own static and Instance constructors.

Case 1. When class A is instantiated.

First the Static constructor of A will be called, then the Instance Constructor.

Case 2. When class B is instantiated

First the Static constructor of the Class B will be called, then the Instance Constructor. One has to explicitly call the construcotrs of class A in the contructors of class B. If constructors of class A are not called (Constructor of Super class is called by SUPER->Constructor in the sub-class constrcutor) in the sub class, then they are not executed. If they are called, then Static constructor of class A will be called first, then Instance Constructor.

Case 3. Whne Class C is instantiated.

Same as Case 2.

I hope your quesiton is answered.

Regards,

Vara

6 REPLIES 6

Former Member
0 Kudos

Hi,

I'm not sure of understanding your question,

anyway

When you create an instance of B it's construcutor was called, but if B hasn't a constructor the A's one is called. (I'm speaking about the instance constructor).

If your question is about which constructor was firs called between the class and the instance I don't now, but you can simple try it.

Bye

enzo

Former Member
0 Kudos

The static constructor is fired before all the instance constructors.The instance constructor is triggered after each instance of the class is created.

Constructors are special methods that cannot be called using CALL METHOD. Instead, they are called automatically by the system to set the starting state of a new object or class. There are two types of constructors - instance constructors and static constructors. Constructors are methods with a predefined name. To use them, you must declare them explicitly in the class.

The instance constructor of a class is the predefined instance method CONSTRUCTOR. You declare it in the public section as follows:

METHODS CONSTRUCTOR

IMPORTING.. [VALUE(]<ii>[)] TYPE type [OPTIONAL]..

EXCEPTIONS.. <ei>.

and implement it in the implementation section like any other method. The system calls the instance constructor once for each instance of the class, directly after the object has been created in the CREATE OBJECT statement. You can pass the input parameters of the instance constructor and handle its exceptions using the EXPORTING and EXCEPTIONS additions in the CREATE OBJECT statement.

The static constructor of a class is the predefined static method CLASS_CONSTRUCTOR. You declare it in the public section as follows:

CLASS-METHODS CLASS_CONSTRUCTOR.

and implement it in the implementation section like any other method. The static constructor has no parameters. The system calls the static constructor once for each class, before the class is accessed for the first time. The static constructor cannot therefore access the components of its own class.

Former Member

HI,

Assume that all the three classes have their own static and Instance constructors.

Case 1. When class A is instantiated.

First the Static constructor of A will be called, then the Instance Constructor.

Case 2. When class B is instantiated

First the Static constructor of the Class B will be called, then the Instance Constructor. One has to explicitly call the construcotrs of class A in the contructors of class B. If constructors of class A are not called (Constructor of Super class is called by SUPER->Constructor in the sub-class constrcutor) in the sub class, then they are not executed. If they are called, then Static constructor of class A will be called first, then Instance Constructor.

Case 3. Whne Class C is instantiated.

Same as Case 2.

I hope your quesiton is answered.

Regards,

Vara

First of all, lets understand how the triggering sequence works in different scenarios. Suppose, if you have both static and instance constructors in all the three classes and assuming that "Super" keyword is used as well in the subclasses. So, now, lets see the possible cases :

1. If Instance of Class A is created : First, remember that static components loads when the Class loads at first place and Instance contructors gets called once the object gets created. So, when you create an instance of the Class A here, then first Static constructor will be called and then the instance construtor will be called.

2. If Instance of Class B is created : Now, when there's no Super keyword then : Static constructor of parent will be called, static of the child will be called and then instance constructor of the child will be called.
Now, if there's Super keyword used inside child classes, then, Static constructor of parent will be called, static of the child will be called and then instance constructor of the parent will be called and finally, the instance constructor of the child will be called.

3. If Instance of Class C is created : Now, when there's no Super keyword then : Static constructor of parent will be called, static of the child class B, then static of the child class C will be called and then instance constructor of the child class C will be called.

Now, if there's Super keyword used inside child classes, then, Static constructor of parent Class A will be called, static constructor of the child Class B will be called, static constructor of the child Class C will be called and then instance constructor of the parent Class A will be called, instance constructor of the parent Class B will be called and finally, the instance constructor of the child class C will be called.

If you found my answer useful, do upvote.

Regards,

Shwetank.

0 Kudos

Thanks for coming to SAP Community for answers. Please post your question as a new question here:

Since you're new in asking questions here, check out our tutorial about asking and answering questions (if you haven't already), as it provides tips for preparing questions more effectively, that draw responses from our members.

Please note, that your post here won't be answered.