Just revising for the SCJP and come accross a question about initialisation blocks. I have copied the following code from the SCJP Sierra bible making slight changes:
class Bird { {System.out.println("1");} public Bird() { System.out.println("2"); } }//End Bird
class Raptor extends Bird { static {System.out.println("3");} public Raptor() { System.out.println("4"); } {System.out.println("5");} static {System.out.println("6"); } }//End Raptor
public class Hawk extends Raptor {
public static void main(String[]args) { System.out.println("7"); new Hawk(); System.out.println("8"); } public Hawk() { } }//End Hawk
When I answered the question I was under the impression that the instance initialisation blocks ran after the super constructor ran or is just the call to the super()? When I ran the code the instance initialisation block runs first and then the Bird() constructor although this sounds correct??
This is the output:
3 6 7 1 2 5 4 8
Is the order as follows:
1) Static init blocks - as class is loading from main() 2) Initialisation blocks - as instance created 3) Super Constructor run
I'm confused because of the fact there are 3 classes??? Can anyone please clarify asap please?
1) Static init blocks - as class is loading from main() 2) Initialisation blocks - as instance created 3) Super Constructor run
When the class is loaded, it initialises the static blocks. When the object is constructed, it initialises the instance block and then the constructor.
The hierarchy is as follows: Bird Raptor extends Bird Hawk extends Raptor
1. static initializer blocks run first, starting on top of the hierarchy (if any), order is important within a class. -> Output 3 and 6
2. Output 7 is obvious...
3. Afterwards, starting again on top of the hierarchy, * first the instance block * then the constructor and * then it returns the control flow to the calling (sub-)class This gives: 1 2 5 4
With other words, every constructor * either calls the super-constructor * or creates an instance - and therefore executes the instance initializer block if any - before it executes commands in the constructor!
This means, that if there is a super-class, then the constructor * first calls the super-constructor and * when control returns, it calls the instance initializer block and * then the commands within the constructor.
Hope it's not too confusing...
I modified your example a little bit to show also other aspects of the problem:
This gives: bird static raptor static 1 raptor static 2 hawk static hawk before instantiation bird instance bird constructor raptor instance raptor constructor hawk instance hawk constructor hawk after instantiation
well!!!I have a doubt on the above concept of initialisation please if anyone could clear it......
First of all link shown by Manfred,there i found example which i tried by modifying a bit...as shown below.....
why here method gets invoked than only static blocks get executed?
the output is as below
Testtry Invoking Derived constructor... ok Base static initialization Block 1. Base initializing static variable 1. Base static initialization Block 2. Base initializing static variable 2. Derived static initialization Block 1. Derived initializing static variable 1. Derived static initialization Block 2. Derived initializing static variable 2. Base instance initialization Block 1. Base initializing instance variable 1. Base instance initialization Block 2. Base initializing instance variable 2. Base constructor body. Derived instance initialization Block 1. Derived initializing instance variable 1. Derived instance initialization Block 2. Derived initializing instance variable 2. Derived constructor body. But later in the example shown
output: bird static raptor static 1 raptor static 2 hawk static hawk before instantiation bird instance bird constructor raptor instance raptor constructor hawk instance hawk constructor hawk after instantiation But later the output varies,as inheritance is implemented here......... now i am gonna totally confused with answer,please if anyone could explain me......
Manfred Klug
Ranch Hand
Joined: Jun 04, 2007
Posts: 377
posted
0
Hi dhwani,
could you formulate your doubt in one sentence? Currently I'm not sure what your problem is.
dhwani mathur
Ranch Hand
Joined: May 08, 2007
Posts: 621
posted
0
Hi Manfred!!
Actualy my doubt is ..In the first example which i have shown the class with main method Testtry does not extend any other class...so its static block gets executed first than the methods disp get executed later were it finds instance of class Derived than only...static blocks and initialisation blocks get executed........
Later in the second example class Hawk with main method extends Raptor so the super class static block get executed..than initialisations block and so on.....
But as per i understand it is mentioned that as class loads all static blocks in program get executed before the program jumps to the main method,and also initialisation blocks get executed later the statements in main method get invoked......
but in the above two programs,it is shows that if inheritance is implemented ie if class extends another class than only static blocks in super class get executed,otherwise if inheritance is not implemented in a class all the static blocks of this class get executed than methods in main method get executed,later if it finds instance of any class than that classes staic blocks get executed...........
please if anyone could explain me......what is the reason for this......it will kinda of you...
Manfred Klug
Ranch Hand
Joined: Jun 04, 2007
Posts: 377
posted
0
Hi dhwani,
The static initialization is executed when the class is loaded, and a class is only loaded when it is needed.
In the first example, the JVM loads and initializes the Testtry class. But since it doesn't extend any class, no further initialization takes place. When you instantiate the derived class, it is loaded and initialized (static and instance).
In the second example, the Hawk class extends another class. As result, the JVM loads and initializes the parent classes too, and when you instantiate the Hawk class, the static initialization is already done.
I hope I got your Question. [ July 27, 2007: Message edited by: Manfred Klug ]
dhwani mathur
Ranch Hand
Joined: May 08, 2007
Posts: 621
posted
0
Hey Manfred!!!Thanks...now my doubt is cleared.....