In my last post the confusion was, I was calling Super class constructor, and it was finishing first then my base class constructor, I will put this in a summary,
1>In a class static code(initializer, block or variable) get executed only once for that class
2>The point no.1 happens whenever the class is loaded
3>It will be executed in the order of its description
4>The class is loaded whenever, a static variable is initialized or static method is called or object of that class is created.
5>Whenever the object of the class is created the constructor is called, in the order up in the hierarchy example A<-B<-C. upon object creation of class C it will call default constructor (assuming no other constructors exists in A,B,C) of B which inturn calls default constructor of A.
6>Now all the default initializer blocks and instance variable initialization gets completed for A in the order of existence. Constructor A finishes control flows back to B, now Constructor B follows the same step and finishes. Now its the turn of Constructor C it also follows the same steps.
7>Step no. 5 and 6 will be repeated every time when object for class c gets created.
The confusion for me was I saw the constructor A finishing first and then, initialization of instance variable in B happening, I got mixed up the 2. I fixed it by adding following code to B.
Code: Select all
B()
{
System.out.println("Boo");
}
To Admin, Sorry in my previous message I accidently added Quote instead of Code, the 2 tabs are next to each other.
Hope this explanation doesn't confuse anyone.
Regards,
GPAR