• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please explain the output

 
indra negi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

 
Akanksha Mittal
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I was wrong here.
 
Akanksha Mittal
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jaydeep and Akanksha.

I was confused about the order of "pre" rest for rest I was sure for their occurence in the output.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic