Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In what order are Java class variables initialised?

Tags:

java

I came across these questions recently, and could not find the answer on StackOverflow;

  1. In what order are Java class variables initialised?
  2. And the somewhat related question, could re-ordering the variables change class behaviour?
  3. Why?

As suggested on Meta I will be posting my answer to this question.

like image 876
Paul Wagland Avatar asked Jan 26 '10 10:01

Paul Wagland


1 Answers

In Java, class variables are initialised in the following order:

  1. Static variables of your superclasses
  2. All static variables of this class are set to their default values.
  3. Static variables, and static initialisation blocks, in declaration order.
  4. Instance variables of your superclasses
  5. All instance variables of this class are set to their default values.
  6. Instance variables, and instance level initialisation blocks, in declaration order

1 & 2 are only done the very first time that a class is instantiated.

So, given the following code:

class Test
  extends TestSuper
{
  final int ti1;
  final int ti2 = counter ++;
  { ti1 = counter ++; }
  static final int ts1;
  static final int ts2 = counter ++;
  static { ts1 = counter ++; }

  public static void main(String[] argv) {
    Test test1 = new Test();
    printTest(test1);
    Test test2 = new Test();
    printTest(test2);
  }
  private static void printTest(Test test) {
    System.out.print("ss2 = " + test.ss2);
    System.out.print(", ss1 = " + test.ss1);
    System.out.print(", ts2 = " + test.ts2);
    System.out.println(", ts1 = " + test.ts1);
    System.out.print("si2 = " + test.si2);
    System.out.print(", si1 = " + test.si1);
    System.out.print(", ti2 = " + test.ti2);
    System.out.println(", ti1 = " + test.ti1);
    System.out.println("counter = " + test.counter);
  }
}

class TestSuper
{
  static int counter = 0;
  final int si1;
  final int si2 = counter ++;
  { si1 = counter ++; }
  static final int ss1;
  static final int ss2 = counter ++;
  static { ss1 = counter ++; }
}

Then we get the following output:

ss2 = 0, ss1 = 1, ts2 = 2, ts1 = 3
si2 = 4, si1 = 5, ti2 = 6, ti1 = 7
counter = 8
ss2 = 0, ss1 = 1, ts2 = 2, ts1 = 3
si2 = 8, si1 = 9, ti2 = 10, ti1 = 11
counter = 12

From this output we can see that the fields are initialised in the order specified in the list.

Now, as to the second question, can re-ordering the fields change the class behaviour. Yes, by re-ordering the fields you change the initialisation order of the fields. Now, in the specific case where all of the fields are independent, this won't affect the observed behaviour, however whenever the fields are not independent, for example in the above code, then re-ordering the fields could change their initialised values.

For example, if the three lines:

  static final int ss1;
  static final int ss2 = counter ++;
  static { ss1 = counter ++; }

are changed to:

  static final int ss1;
  static { ss1 = counter ++; }
  static final int ss2 = counter ++;

Then the output would change to:

ss2 = 1, ss1 = 0, ts2 = 2, ts1 = 3
si2 = 4, si1 = 5, ti2 = 6, ti1 = 7
counter = 8

That is, ss2, and ss1 would change values.

The reason for this is that this behaviour is specified in the Java Language Specification.

like image 92
Paul Wagland Avatar answered Sep 27 '22 17:09

Paul Wagland