aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object constructor and initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object constructor and initialization" Watch "Object constructor and initialization" New topic
Author

Object constructor and initialization

Yi Meng
Ranch Hand

Joined: May 07, 2003
Posts: 270
What's the output of the code below:


Meng Yi
Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
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 ]

Narasimha
Rajinder Yadav
Greenhorn

Joined: May 13, 2003
Posts: 27
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 ]

When faced with an easy thing to do and a hard thing to do, always pick the right thing to do!<p><a href="http://yadav.shorturl.com" target="_blank" rel="nofollow">Rajinder Yadav</a>
Mandar Puranik
Ranch Hand

Joined: Jan 08, 2003
Posts: 35
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 ]

To Bug is Human,<br />To Debug Divine... :-))
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Mandar, you might find interesting Order of initialization by Bruce Eckel.


SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object constructor and initialization