GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Execution Order of Static, Init, Instance Blocks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Execution Order of Static, Init, Instance Blocks" Watch "Execution Order of Static, Init, Instance Blocks" New topic
Author

Execution Order of Static, Init, Instance Blocks

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
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


Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Borja Campina
Greenhorn

Joined: Jan 09, 2010
Posts: 4


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.
Sridhar Gudipalli
Ranch Hand

Joined: Nov 02, 2005
Posts: 120
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|SCJP 6.0
SCWCD objectives
Sridhar Gudipalli
Ranch Hand

Joined: Nov 02, 2005
Posts: 120
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

Joined: Feb 18, 2010
Posts: 434
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!
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
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 ~!!!


SCJP 6.0 96%

(Connecting the Dots ....)
Ulrich Vormbrock
Ranch Hand

Joined: Apr 15, 2010
Posts: 73
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...


SCJP 6 (88%), SCWCD (89%)
 
Consider Paul's rocket mass heater.
 
subject: Execution Order of Static, Init, Instance Blocks