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}};

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??

Recommended Answers

All 2 Replies

What prints out if you put a println in the blocks?

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).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.