aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes initialization sequence - what is done first? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "initialization sequence - what is done first?" Watch "initialization sequence - what is done first?" New topic
Author

initialization sequence - what is done first?

Shailendra Guggali
Ranch Hand

Joined: Feb 01, 2001
Posts: 86
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
Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
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
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
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
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
Thanx Val - i got it
Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
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
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.
quan zhu
Greenhorn

Joined: Dec 14, 2000
Posts: 27
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
Yeggy Easwaran
Greenhorn

Joined: Jan 25, 2001
Posts: 9
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
Deepak Jindal
Ranch Hand

Joined: Nov 16, 2000
Posts: 30
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
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
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: initialization sequence - what is done first?