• 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

Order of initialization

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

In my memory, static block is execute when class file be loaded in JVM,hence static { x /= 5; }should be failed;but i run this code that got answer,so anyone can tell me the order of initialization?Thanks
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JVM first executes static initializers for StaticStuff ancestors recursively. Then, executes static initializers for your StaticStuff. Later, executes the main() methodh so you can see the output with variable�s value computed.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will
Check the JLS section 12.5 the exact steps and the exact order things are done in.
 
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Order of code execution
1.static variables initialization.
2.static initializer block execution. (in the order of declaration, if multiple blocks found)
3.constructor header ( super or this – implicit or explicit )
4.instance variables initialization / instance initializer block(s) execution
5.rest of the code in the constructor
Interfaces
Clement
 
Steven Wong
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, pls ignore the last word "Interfaces".
Type error....
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Clement Ng:
Hi,
Order of code execution
1.static variables initialization.
2.static initializer block execution. (in the order of declaration, if multiple blocks found)
3.constructor header ( super or this – implicit or explicit )
4.instance variables initialization / instance initializer block(s) execution
5.rest of the code in the constructor
Interfaces
Clement


This order of execution is really helpful, I actually copy it down.
But I have a question related to this. Is this statement true or false: "The default constructor initializes the instance variables declared in the class. " Looks like instance variables intitialized at the time constructor is executed(between step 3 and 5), but is it OK to say constructor initializes the instance variables??
 
WiLL Tao
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks folks who join this thread,now I found good trace tool from JDK
javap.



This tool indicates the step when class execute,right?
[ April 17, 2002: Message edited by: WiLL Tao ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic