File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes initialisation sequence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "initialisation sequence" Watch "initialisation sequence" New topic
Author

initialisation sequence

preeti dengri
Ranch Hand

Joined: Nov 30, 2000
Posts: 111
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

Joined: Nov 06, 2000
Posts: 1510
Yes, you are correct. That's the sequence.
Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Sam Wong
Ranch Hand

Joined: Dec 07, 2000
Posts: 133
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

Joined: Nov 04, 2000
Posts: 135
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

Joined: Dec 07, 2000
Posts: 133
I would say 3 and 4 should be swapped.
Neha Gupta
Greenhorn

Joined: Dec 04, 2000
Posts: 9
Could you please sight an Example Nasir..


Neha
Nasir Khan
Ranch Hand

Joined: Nov 04, 2000
Posts: 135
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

Joined: Nov 04, 2000
Posts: 135
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

Joined: Dec 07, 2000
Posts: 133
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: initialisation sequence