• 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

Execution Order of Static, Init, Instance Blocks

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Somehow, I am having difficulty with this concept.

At the end of Chapter 3 of Sierra/Bates, they give code:




When I traced it, I got the output:

pre r1 r4 b1 b2 r3 r2 hawk

However, the book says output is:

r1 r4 pre b1 b2 r3 r2 hawk


Why on earth would the static init blocks of Raptor execute before Hawk outputs "pre." Is an instance of Hawk created as soon as the main() function begins running? And if that is the case, why doesn't the static init blocks of Bird execute, then Raptor because Bird is the Superclass of Raptor.

I am really missing the concept here, please clarify
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


To try to help a little more, I show you an example:
Case class B extends A, execution order:
- class a static init block
- class b static init block
- class a instance init block
- class a constructor
- class b instance init block
- class b constructor

Sorry for my english.
 
Ranch Hand
Posts: 120
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lets see the below code. Sysout statements are scattered in the program. Please dont confuse. Just observe the output and find out the rules yourself.

Source: SCJP K&B - Chapter#3, Q#14 (Modified the program to better understand)

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

Sandra Bachan wrote:
Why on earth would the static init blocks of Raptor execute before Hawk outputs "pre." Is an instance of Hawk created as soon as the main() function begins running? And if that is the case, why doesn't the static init blocks of Bird execute, then Raptor because Bird is the Superclass of Raptor.



Please see the explanation in answer (K&B).
1) Static init blocks are executed at class loading tme.
2) Instane init blocks run right after the super() in a constructor.

Sandra Bachan wrote:why doesn't the static init blocks of Bird execute, then Raptor because Bird is the Superclass of Raptor.

Please observe carefully, if the Bird has "static" init block.
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I appreciate the time and effort to explain this to me. I think this is one of my weaker areas, as I had a similar question about a week ago. Will have to study this objective a bit harder.

Cheers!
 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens When class is loaded by JVM

When JVM loads a class into the memory , it executes its static blocks.

What Happens when object is instantiated

When any class's object is created , JVM executes its init blocks followed by constructors.

What happens when an JVM is given a command with new <Some class>

When JVM encounters new <Some class>, it creates an object of its parent class,( Ofcourse, recursively applying this definition means , if this class has a parent, then its parent is created first) and then comes down to create an object of the class to whom we applied new.

Integrate the above three facts , you would get an Answer to your question.

Thanks ~!!!
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here another rule of thumb concerning constructors and "instance init block" - I want to emphasize this because taking several mock exams, I was unable to answer to such questions spontaneously: what comes first - the instance init block or the constructor? How is this related to the call to super() or this()?

There has been one crucial hint which helped me to get a deeper understanding: an instance init block can reduce redundancy ... provided that several constructors (the overloaded ones in the same class) have to share a member.

Consequently, it's obvious that a constructor has to do his work AFTER the instance init block has finished.

Below, please find an example in which a variable called constructorCalls is accessed by multiple constructors. If the instance init block was called only after completion of a constructor, such approach of sharing variables would not make sense.



And here's the ouput:

static init block
FatherOfMultipleConstructors() has been invoked
instance init block
MultipleConstructors(int i, int j) can now access variables from instance init block
MultipleConstructors(int i) can now access variables from instance init block
MultipleConstructors() can now access variables from instance init block
constructorCalls has been accessed by ... MultipleConstructors(int i, int j) MultipleConstructors(int i) MultipleConstructors()



As you can see in the ouput, the instance init block is called after all constructor's calls to super() (or this()) and before the constructors have finished their work.
Hope this explanation is not too confusing...
 
reply
    Bookmark Topic Watch Topic
  • New Topic