Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

initialisation sequence

 
preeti dengri
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the following tip from javaranch FAQ page has confused me
Instance initializer(s) gets executed ONLY IF the objects are constructed.
however I know th esequence to be
1)static initialisation
2)instance initialisation
3)constructor(s)
please correct if i am wrong
 
Bosun Bello
Ranch Hand
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are correct. That's the sequence.
Bosun
 
Sam Wong
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct in the sequence. You may be confused by the use of word "constructed" in the tip. If I reword the tip to be:
Instance initializer(s) gets executed ONLY IF new is called.
So if an object is created via new, then instance initialization and then constructors are executed. Note that static member initialization is done once when the class is loaded. So repeated instance creation will not perform static initialization. However, code residing within a static block will get executed per creation. So be careful with it.
 
Nasir Khan
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Order of code execution (when creating an object)
1. static variables initialization.
2. static initializer block execution. (in the order of declaration, if multiple blocks found)
3. constructor header ( super or this - implicit or explicit )
4. instance variables initialization / instance initializer block(s) execution
5. rest of the code in the constructor
However, I need someone to verify.

[This message has been edited by Nasir Khan (edited December 12, 2000).]
 
Sam Wong
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say 3 and 4 should be swapped.
 
Neha Gupta
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please sight an Example Nasir..
 
Nasir Khan
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, here is an example
class example{
static int i=1;
example(){i++;System.out.println("in constructor value ="+i);}
static {i++; System.out.println("in Static block1 value= "+i);}
{i++;
System.out.println("in instance initializer Block2 value="+i);
}
public static void main(String s[]){
new example();}
static {i++; System.out.println("in Static block2 value= "+i);}
}
 
Nasir Khan
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However I agree with the statement
Instance initializer(s) gets executed ONLY IF the objects are constructed.
but the order of code execution from class loading to object
creation is as i gave above
 
Sam Wong
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's an example on why I think 3 and 4 should be swapped.
<pre>class Base {
static int i = 10;
static {
System.out.println("Base Static Init");
}
{
System.out.println("Base Instance Init");
}
public Base() {
System.out.println("Base: " + i);
i=1;
System.out.println("Base: " + i);
}
}
class Agg extends Base{
static {
System.out.println("Agg static i:" + i);
i=2;
System.out.println("Agg Static Init");
}
{
System.out.println("Agg Instance Init");
}
public Agg() {
this(99);
System.out.println("Agg: " + i);
}
public Agg(int x) {
System.out.println("Agg(int) " + x);
}
}
public class Avf{
public static void main(String argv[]){
for (int i = 0 ; i < 2 ; i++) {
Base a = new Agg();
}
}
}
</pre>
Here's the output of the program:
<pre>
Base Static Init
Agg static i:10
Agg Static Init
Base Instance Init
Base: 2
Base: 1
Agg Instance Init
Agg(int) 99
Agg: 1
Base Instance Init
Base: 1
Base: 1
Agg Instance Init
Agg(int) 99
Agg: 1
</pre>
Order of Initialization
- Static variable declaration/initialization
- Static block
- Instance variable declaration/initialization
- Instance block
- constructor code
But there is a clarification. This order is per class only. All parent classes are loaded in order starting with the root class. So mixing super() and this(???) will be confusing because they produce different results. If you call this(???) in your constructor, the instance block gets executed first, then this(???) and finally the rest of the original constructor code. So order of initialization isn't as simple as your list above though I would still swap 3 and 4.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic