• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Please explain the output

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I was wrong here.
 
Akanksha Mittal
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic