Initializationblocks

Static Initialization blocks

Non Static Initialization Blocks

Order of initialization

1) Static statements/static blocks are executed.
2) Instance variables are assigned default values
3) Instance variables are initialized
4) constructor runs
5) Instance initialization block(s) run after all the call(s) to super has(have) been completed but before the rest of the constructor is executed.
6) Rest of the constructor is executed.

Answer and Correction by Henry Wong
This list is *almost* correct. Item 3 is only true, if the instance variable is assigned a compile time constant. Otherwise, it will be done with Item 5 (instance variables *and* instance initializers will be done together from the top to the bottom, in the order they are defined).

Sample Program

public class MyInitTest {
    public MyInitTest() {
        System.out.println("After Instance Initialization Block");
        System.out.println("constructor called");
 
        s1 = sM1("1");
    }
    static String s1 = sM1("a");
    String s3 = sM1("2");
    {
        System.out.println("Initialization Block running");
        s1 = sM1("3");
    }
    static
    {
        System.out.println("Static Initialization Block");
        s1 = sM1("b");
    }
    static String s2 = sM1("c");
    String s4 = sM1("4");
    public static void main(String args[]) {
        MyInitTest it = new MyInitTest();
    }
    private static String sM1(String s) {
        System.out.println("sM1 called with Parameter " + s);
        System.out.println(s);
        return s;
    }
}

The output of the above program is :

sM1 called with Parameter a
a
Static Initialization Block
sM1 called with Parameter b
b
sM1 called with Parameter c
c
sM1 called with Parameter 2
2
Initialization Block running
sM1 called with Parameter 3
3
sM1 called with Parameter 4
4
After Instance Initialization Block
constructor called
sM1 called with Parameter 1
1

Programs/FAQ

Why the output is not correct?

class tt {
    static String s = "-";
 
    public static void main(String[] args) {
        go();
        System.out.println(s);
    }
    { go(); }//line a
 
    static { 
        go(); 
    }
    static void go() { 
        s+= "s"; 
    }
}

The output is -ss.

Doubt: I'm confused as to why not the line a code is not executed. please help.

Reply from other people:

Praveen Javvaji:
You are invoking a static method in an initializer block, though the compiler won't complain, but the interpreter simply ignores it, it seems.

Try the following code

class tt {
    String s = "-";
    public static void main(String[] args) {
        //go();
        System.out.println(new tt().s);
    }
    { System.out.println("hi"); go(); }//line a
    //static { go(); }
    void go() {
        s+= "s";
    }
}

Here the method is not static, so we can't invoke it from static initializer block, but we can invoke with an initializer block.

from Ian Edwards:

{ go(); }//line a

This is a non-static initialisation block. This line isn't executed because no object is being instantiated. These blocks are only executed when an object is instantiated and are executed in the order that they appear.

In the case of the static initialisation blocks these are executed when the class is loaded.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.