In what order are Java class variables initialized?

I recently came across these questions and could not find an answer to StackOverflow;

  • In what order are Java class variables initialized?
  • And a somewhat related question, can override variable changes to the behavior of a class?
  • Why?

As suggested by Meta , I will post my answer to this question.

+6
java
source share
1 answer

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

  • Static variables of your superclasses
  • All static variables of this class are set to their default values .
  • Static variables and static initialization blocks in declaration order.
  • Instance variables of your superclasses
  • All instance variables of this class are set to their default values .
  • Instance variables and instance-level initialization blocks in declaration order

1 and 2 are executed only the first time the 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 result:

 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 conclusion it is clear that the fields are initialized in the order specified in the list.

Now for the second question, you can reorder the fields to change the behavior of the class. Yes, by reordering the fields, you change the order of initialization of the fields. Now, in the specific case when all the fields are independent, this will not affect the observed behavior, however, whenever the fields are not independent, for example, in the above code, then redefining the fields can change their initialized values.

For example, if there are three lines:

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

change to:

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

Then the output will 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 will change the values.

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

+7
source share

All Articles