• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

order of operations static fields, init statments, constuctors and inheritance

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know I am missing a fundamental here too, this as well comes from Sun Certified Programmer for Java 6 page 318.

Why doesn't pre print out first?

Actual answer:
r1 r4 pre b1 b2 r3 r2 hawk

 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is because the static block of the class and its superclass is always called during the class is first loaded.
When the Hawk class is loaded it is checked, also the superclass is loaded, and as soon as it happens the static block is executed.
After that pre is printed.
 
Chadd Franck
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, is the class first loaded when the main is run?

or when new Hawk(); is run?
 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See the class name you provided during saving your code must contain the main() method.
JVM needs to be told where is the main() located.
When it locates the main() it loads the class and also the super class in the memory.
During this, as we know, the static variables and methods belongs to the class and not to any instance, the static block it executed and only one copy of it is maintained in the memory.

The cal to new Hawk() makes a new object in the JVM, in which the new keyword is responsible for the call to the constructor of the class.

So first the class is loaded along with it superclass in the memory.
It is after this you can do new Hawk().

Hope this helps.
[ December 10, 2008: Message edited by: Sudipto Shekhar ]
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add a little, If you add a static block to Bird class you will see that it gets printed first before all other. So that means all the classes should get loaded (since your Hawk class extends Raptor and Raptor extends ... so on) before main method starts executing.
[ December 10, 2008: Message edited by: Vijitha Kumara ]
 
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SJCP-related question. Moving.
 
reply
    Bookmark Topic Watch Topic
  • New Topic