Forums Register Login

Object constructor and initialization

+Pie Number of slices to send: Send
What's the output of the code below:
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
I would just like to add that the static initialization code block is executed once when the class is loaded.
Also instance initialization code blocks are executed each time an object is created.
Here is a program that i wrote to demonstrate this.
Notice that the static init code block for class foo is executed once, while the instance init block is executed twice.



Output
Static initializer executed in class test
Inside class static method: main()
Static initializer executed in class foo
instance initializer executed when object foo is created
Object getting created
instance initializer executed when object foo is created
Object getting created
[ May 15, 2003: Message edited by: Rajinder Yadav ]
+Pie Number of slices to send: Send
Hi Narsimha and Yi....
I havent understood a part of Yi's code and Narsimha's explanation.
Here is it again :


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,....


Why does the control come to line 3 and not above that ? coz u say that all the instance blocks and instance variables will be executed, then why not the control goes again to line 1 ?
(The whole block is static,so either it should be executed once, or how come only the first method is executed in the second part ? I am damn confused )
Please clarify..
Thanks
Mandar
[ May 16, 2003: Message edited by: Mandar Puranik ]
+Pie Number of slices to send: Send
Mandar, you might find interesting Order of initialization by Bruce Eckel.
And then the flying monkeys attacked. My only defense was this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1058 times.
Similar Threads
Practice Question: What is the output?
[this]....
can't call another constructor from no-arg constructor
Semicolon in string?
practice question: what is the output?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 23:03:00.