Take a look at this code : Could anyone explain the flow of control? public class InitTest { static String s1 = sM1("a"); { s1 = sM1("b"); } static {s1 = sM1("c"); } public static void main(String args[ ] ) { InitTest it = new InitTest( ); }
private static String sM1(String s) { System.out.println(s) ; return s; } } Answer: c, a, b
First always gets initilized a declaration that starts with static infornt of it not in static block. Then followed by a stotic block and only then by a member block.
You are right val the answer is a, c, b - but in the main when we instantiate the class - since there is no constructor - what does it do. does the control pass to variables declared static --- to static blocks -- and then to init blocks?? So i tried by putting the String in a constructor - it then goes to the static block first ---> initializer block ---> Constructor Suppose we remove the reference to the method. it the starts with the Static block ---> Initializer block. how does it work???
If you haven't specified a constructor a default one will be provided for you by the compiler. So the initilization will still proceed as before. Here take a look at this with a constructor. Same output as before.
ok you want to see what will be shown if you remove the method calls ? It appears that intilize block will be called first in this case if there is no method calls. Followed by a and b. public class InitTest { static String s = "a"; static String s1 = "a"; { s1 = "b"; System.out.println(s1); } static { s1 = "c"; System.out.println(s1); } public InitTest() { System.out.println("What happened"); } public static void main(String args[ ] ) { System.out.println(s) ; InitTest it = new InitTest( ); } }
It's still interesting how it happends though i am not quit shure why the static block get's called first. If that happends that means that the s1 hasn't been initilized yet and it's forward referencing if you get to use s1 in static block. It gives no error so i think that still the static iditentifier that's not inside a static block still loads first otherwise i don't see how this works. I hope someone can answer this better
I just remembered that the identifiers are called in the order that they are written. So first all the statics(we already know that) then all the members. So s1 will be initilized first and only then the static block and members. Sorry i misslead you there.
precondition: 1)With method call, 2)has a default constructor 3)has a super class the output is: 1)static declare 2)static block 3)super class constructor 4)initializer 5)constructor why super constructor is called before initializer
Hi quan, Whenever an object is created with the "new" keyword,a call is made to the constructor. If the object has any superclasses,then a call is immediately made to the superclass constructor and this goes on until we go all the way up the inheritance hierarchy. Say ,there is just one superclass,then when the superclass constructor is called 1) a)if the superclass has any initalizer block,then that is executed first and then the constructor is executed. b)The initializer block in the subclass is executed and then the subclass constructor is executed. 2)If the Superclass itself has some static initializers then that is executed first before everything else (i.e. even before the static initializers of the subclass) and then the static initializers of the subclass are executed,followed by Initializers and so on and so forth....... So the order can be put as follows: Superclass:Static Decl Superclass:Static Block Subclass:Static Decl Subclass:Static Block Superclass:Initializer Superclass:Constructor Subclass:Initializer Subclass:Constructor
For example:- ------------ class bb{ static { String superstaticvar= "e"; System.out.println("super static value=" + superstaticvar); } String initvar = "a"; { initvar = "d"; System.out.println("super initializer value =" + initvar); } bb(){ System.out.println("Inside super constr"); } } public class InitTest extends bb { static String s1 = sM1("a"); { s1 = sM1("b"); } static {s1 = sM1("c"); } public static void main(String args[ ] ) { InitTest it = new InitTest( ); } public InitTest() { System.out.println("Inside sub constr"); } private static String sM1(String s) { System.out.println(s) ; return s; } } Hope all this makes sense -Yeggy
Yeggy<br />Sun Certified Programmer for Java2 Platform
It's like i said in my last post , the order of initilization will proceed as how it was declared. First always static then member initilization. It doesn't matter of order how the initilizers are written. Daapak , string is final but whenever you assign a new value to a string the old value that it was referencing is disgarded and only references a new value.
Val SCJP <BR>going for SCJD
A timing clock, fuse wire, high explosives and a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth