• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sequence execution of initilization block,static block,constructor

 
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please correct me if i am wrong?

<code>

</code>



<output>
compile-single:
run-single:
one
two
fuu
3
4
5 constructor
o1
o2
Test sub class constrcutor
BUILD SUCCESSFUL (total time: 0 seconds)

</output>


It seems that
1) static blocks gets executed first in order they are declared in superclass then sublass.
2) initlization blocks of superclass followed by superclass constructor.
3)initilization blocks of subclass followed by supclass constructor


Please let me know this is a correct seq or something is remaining?

Any trick to remmber this for exam?



[BSouther: Added UBB CODE tags]
[ January 27, 2008: Message edited by: Ben Souther ]
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes that appears to be correct.

What you need to remember is that before an object can be created the classes first need to be loaded by the class loader. This is when the static initialisers are executed. Once they are completed the non-static initialisers and constructors can be executed. For each class in the hierarchy execute the non-static initialisation block then its constructor.
 
reply
    Bookmark Topic Watch Topic
  • New Topic