• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

initialization sequence

 
Tualha Khan
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,
Hey,
What is the correct sequence of execution of Super Class - Sub Class:

Super class's Static initializer//1
Super class's instance intializer//2
Super Class's Constructor//3
SUB class's instance intializer//4
SUB class's Static intializer//5
SUB class's Constructor//6

I have a code which gives the above above sequence.
Why is the sub class's static initializer (5) initialized after the sub class's instance initializer. Is it because the call to the constructor of the sub class, gives the call super(), which when returned, goes back to the constructor of the sub class.
Why is this happening???
Please Clarify.
The code for the above result is followed.
Thanks a lot!!
Bye,
Tualha Khan

*****************************************
class tt1
{
{
System.out.println("In Super class's instance intializer");
}
static
{
System.out.println("In Super class's Static initializer");
}
tt1()
{
System.out.println("In Super Class's Constructor");
}
}
class tt2 extends tt1
{
{
System.out.println("In SUB class's instance intializer");
}
{
System.out.println("In SUB class's Static intializer");
}
tt2()
{
System.out.println("In SUB class's Constructor");
}
public static void main(String args[])
{
new tt2();
}
}
***************************************
 
Vijay pillai
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
the order of initialisation is always the super class static initializer,then the subclass static initializer,the instance ini of the super then is the call to the constructor of the super made.
followed by the call to the ini if any of the sub class.
the code u have presented in sub doesn't have a static ini instead its the 2nd instance ini printing.
thanks
Vijay Pillai
 
Kedar Jog
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
U are missing static keyword in the init sequence in the child class
-Kedar
 
Tualha Khan
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Such a silly mistake, my future boss will fire me anything!!
Bye,
Tualha Khan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic