When Hawk.main() is executed, Hawk.class must be loaded. For this, its super classes must be loaded as well. So Bird is loaded followed by Raptor followed by Hawk. When a class is loaded, its static members are initialised and static code segments are executed. (But the contsructor is not yet executed). This will mean static constructs of Bird are executed followed by static constructs of Raptor and then Hawk. It so happens that the Bird class has no static constructs, Raptor has two (so they are executed in the sequence in which they appear) and Hawk has none. Hence you see
At this point because no instance of any class is created no other statments have been executed. Next statement to execute is the sout in Hawk.main() which means the output now reads
r1 r4 pre1
After this an object of the type Hawk is created, when this happens, constructors and non static code fragments are exeucted.
the non-static class level code fragments (i forgot what is the official name for these) execute in the order which they are defined but before the constructor. So you see
r1 r4 pre1 b1
The then constructor of Bird class,
r1 r4 pre1 b1 b2
Then the non-static class level code fragments of Raptor
r1 r4 pre1 b1 b2 r3
Then the constructor of Raptor
r1 r4 pre1 b1 b2 r3 r2
And finally, statement after the creation of object of Hawk.
I hink I speak for everyone who creates mock exams when I say that we encourage you to post mock exams on this forum and discuss them, but we really appreciate it when you indicate where you got the question!
In other words, just say "this question came from whizlabs, or Dan Chisolm, or K&B" something like that.
Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
@Ritesh very well explained and @Ali thank you for posting this question also could you tell me where you found it?
Although I wanted to clarify my understanding regarding the execution of initialization blocks.
Starting from super class down the tree to the child class
1) static initialization blocks execute before anything and only once.. and in the order they are coded(in case of multiple static init blocks).
2) then the non static initialization blocks execute every time an object is being created and in the order they are coded(in case of multiple static init blocks).
3) then the constructor executes.
Vishal Kashyap wrote:Dear Kakani,
I thought(NOT SURE, please confirm it more) that Initialization blocks are class specific so we could not inherit them.
Initialization happens when you create a object a class, and it's not inherited to sub class, because, it's related to constructor.
static initialization happen when the class is loaded, and the object creation order is below.
1. Set fields to default initial values (0, false, null)
2. Call the constructor for the object (but don't execute the body of the constructor yet)
3. Invoke the constructor of the superclass
4. Initialize fields using initializers and initialization blocks
5. Execute the body of the constructor.
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Joined: Oct 14, 2002
Hey Vishal and "K"
I don't know if you noticed but this discussion was abandoned four years ago!
It's best to let old threads be, and not add new posts to them. It's certainly fine to read them, but bringing them back to life tends to cause confusion.
Sorry Respected Sir(Bert),
I really don't know; one of the reason is, I am just a kid in front of you and on the land of Java [regarding Knowledge]. Simply trying to learn; how to stretch my wings and fly frequently under guidance of JavaRanch in this Java Sky; because each and every thing is Java here. Thanks for your suggestions, Sir.
L K Kakani
Joined: Aug 30, 2010
Hello Mr. Bates,
Didn't notice it until I saw your post. Sorry about that.... I am new to this forum and will notice the date from now on before posting any replies/questions .
PS: Thank you so much for all the wonderful books on Java and also for helping us Java greenhorns get better
subject: Problem in understanding output of Program