I'll try to explain things, il will be a little long:
1) You invoke the main method in class Animal2, so class Animal2 is loaded. 2) The JVM sees that Animal2 is extending zion, so class zion is loaded. 3) Static initialisers fron class zion are invoked: there are none. 4) Static initialisers from class Animal2 are invoked: "C's static" is printed. 5) You enter the main method. 6) new Animal2() is invoked. 7) The no-arg constructor wants to invoke the constructor that takes a String, but before that the makeRandomName() method is called in order to produce a random name. 8) In makeRandomName(), "static" is printed and a random name from the array is returned. 9) We enter Animal2(String name), where super() is invoked explicitly. Note that it would have been invoked anyway, even if not written explicitly. Also note that, because we didn't yet return from the super() call, no instance variable initialisation took place yet. 10) We enter zion(). First, super() is called implicitly (which is the no-arg constructor of class Object), then the instance variable initialisation for zion must take place. 11) As we said above, String temp is initialised, and because getString() is static it can be called and it prints "getstring". temp is initialised with "string returned". 12) The instance variables are initialised, we continue with the zion() constructor, which prints "In zions constructor". 13) We return to Animal2(String name), now the instance variable initialisation must take place, because we just returned from the superclass constructor. 14) i is initialised by calling InitialValue(), which prints "Initial" and initialises i to 0. 15) name is initialised to null. 16) We continue with the constructor, which assigns to name the random value calculated at points 7) and 8). 17) We return to the no-args constructor, which prints "this". 18) The object has been finally constructed, we return to main and print its name attribute (random name from the array of dog names). 19) new Animal2("Zeus") is invoked. Points from 9) to 15) are happening again for the new object creation. 20) name is assigned the value "Zeus". 21) Back to main, where we print the name atribute "Zeus". 22) End.
What I don't understand very clearly, is why at point 14) we are allowed to call InitialValue(), which is an instance method, even if the object is not yet constructed (we are still in the constructor).
Hope this helps, [ August 03, 2007: Message edited by: Radu Zaharia ]
Instance variable initialization will take place just after the return from the superclass constructor call. Then the constructor continues with the rest of its code. And yes, all the instance variables get initialised before the constructor ends.
For example, in the case of class Animal2, if we call the no-arg constructor, it will call the String constructor (in line //1), the String constructor will call the superclass constructor (in line //3), then the instance variable initialization will take place (between lines //3 and //4). After that, the constructor continues with line //4, returns to the no-arg constructor which continues with line //2 and only then we have finally constructed our object.