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.

Val SCJP <BR>going for SCJD

Shailendra Guggali
Ranch Hand

Joined: Feb 01, 2001
Posts: 86

posted Feb 02, 2001 11:33:00

0

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???

Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439

posted Feb 02, 2001 11:38:00

0

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.

Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439

posted Feb 02, 2001 11:45:00

0

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( ); } }

Shailendra Guggali
Ranch Hand

Joined: Feb 01, 2001
Posts: 86

posted Feb 02, 2001 11:53:00

0

Thanx Val - i got it

Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439

posted Feb 02, 2001 12:21:00

0

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

Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439

posted Feb 02, 2001 12:40:00

0

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.

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

I am getting a bit confused here. I think the String is immutable. So won't the codes written above give compilation error. Thanx in advance.

Yeggy Easwaran
Greenhorn

Joined: Jan 25, 2001
Posts: 9

posted Feb 02, 2001 14:50:00

0

Deepak.. Why don't you go ahead and compile to see if there is a compilation error?? What is the confusion about? Yeggy

Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439

posted Feb 02, 2001 15:52:00

0

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.