This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

initialization sequence - what is done first?

 
Shailendra Guggali
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 439
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shailendra Guggali
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 439
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 439
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Val - i got it
 
Val Dra
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 439
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Deepak Jindal
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 439
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic