0
public class Twisty
{
    { index = 1; } //need clarification here

    int index;

    public static void main(String[] args)
    {
        new Twisty().go();
    }

    void go()
    {
        int [][] dd = {{9,8,7}, {6,5,4}, {3,2,1,0}};
        System.out.println(dd[index++][index++]);
    }
}

In the above code, inside the instance initialization code, index is used. But it is declared only after the block. This code compiles fine but i just need more detail to understand this. Is it that the class will first load all its instance variables before invoking any init blocks?? what if the code contained any static init blocks?? which one willl load first??

3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by JamesCherrill
0

Any static initialisers are executed in the order in which they are declared before the class is first used.
Any non-static initialisers are executed in the order in which they are declared, and before the constructor, when an instance us created.
The order in which variables are declared isn't important unless they are initialised in their declarations, in which case you can have problems with circular references (this is rare).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.