Hi Yi,
Let me explain clearly. I am copying your whole programme with the line numbers, so going forward it will be easy to understand.
-------------------------------------
class A
{
static {//1
System.out.println("sa");//2
}
{//3
System.out.println("ia");// 4
}
A(){// 5
System.out.println("ca");// 6
}
};
class
Test extends A
{
static {// 7
System.out.println("st");// 8
}
{// 9
System.out.println("it");// 10
}
Test(){// 11
System.out.println("ct");// 12
}
public static void main(
String args[])
{
System.out.println("m1");// 13
Test t=new Test();// 14
System.out.println("m2");// 15
}
}
-----------------------------------------------
Output:
sa
st
m1
ia
ca
it
ct
m2
-----------------------------------
Now come to the flow of execution
If i specifies some basics which you know very well please ignore the same ). First programme exe(my notation for excution) starts at line no(my notation for number) 13, before exe line 13, it will check whether the class is having static initializer blocks, static variable declarations or static variable nitializations. Here the class Test is having static initializer block declared on line no 7, so control goes to the line no 7, before executing this block, it will check whether my class is extending any class, here the class 'Test' is extending class 'A', so control goes to the super class, here 'A' and will excute the static blocks if any. So the execution starts on the line 1, hence line no 2 will be executed and will print "sa". Now there are no more static blocks or static initializations, hence control comes to the sub class 'Test" and will start the excution of static blocks if any, so execution starts at the line no 7 and will excute the line no 8, hence "st" will be printed next. Now all the static blocks are completed in the sub
class as well as in the super class, control will comes to the line no 13 and the same will be executed , hence the next output will be "m1". Now control comes to the line no 14, and will try to create the instanc e of the class 'Test". Before creating any instance all the instance blocks and instance variables will be excuted in the order, before doing this same thing will be done in the super class. Hence control goes to the line no 3 and line no 4 will be executed, after this control goes to the constructor and line no 6 will be executed.( Here while creating the instance, you are using the no parameters constructor i.e., default constructor. And the default constructor in the sub class is not calling explicitly any constructor, hence super class default constructor,(line no 5) will be called. ) After this control comes to the sub class, and instance intializer block at line no 9 will be executed after this default constructor will be executed.After successfully creating the class instance i.e., executing the line no 14, obviously the next statement 15 will be executed.
Sorry, if you didn't understand any thing, actually i want to type more clearly, but bec of time constraint i couldn't do the same.
One request for other members, if my understanding is wrong somewhere, please do correct me, i will be thankful to you.
[ May 15, 2003: Message edited by: narasimha rao bandlamudi ]