File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please explain the output 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 "Please explain the output" Watch "Please explain the output" New topic
Author

Please explain the output

indra negi
Ranch Hand

Joined: Aug 12, 2009
Posts: 68

Please explain why the output of the following code is : r1 r4 pre b1 b2 r3 r2 hawk

Akanksha Mittal
Greenhorn

Joined: Jul 29, 2009
Posts: 26
Sorry I was wrong here.
Akanksha Mittal
Greenhorn

Joined: Jul 29, 2009
Posts: 26
So, the order of execution is like this

1. Static initialization blocks
2. Initialization blocks
3. Constructors

I was wrong in the first place.. Thanks for your question..
Jaydeep Mazumdar
Greenhorn

Joined: Aug 26, 2009
Posts: 23
The output "r1 r4 pre b1 b2 r3 r2 hawk" is described below:

1. Class Hawk and all its parent classes are loaded into memory. As soon as it loads up all static initialization blocks are executed. This explains "r1" and "r4".
2. The main method is executed next. This prints "pre".
3. You create an instance of Hawk. As Hawk extends from Raptor and Raptor from Bird, the Hawk constructor needs to initialize both Bird and Raptor in the same order. This prints "b1" "b2" "r3" and "r4".
4. Finally, the last statement in main is executed. This prints "hawk".


Hope this helps! Any queries, get back to me.

indra negi
Ranch Hand

Joined: Aug 12, 2009
Posts: 68
Thanks Jaydeep and Akanksha.

I was confused about the order of "pre" rest for rest I was sure for their occurence in the output.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Please explain the output