When a type is initialized, all static initializers and all static field initializers are executed in textual order. From JLS 12.4.2 :
Then execute either initializers of the class variable, and initializers of the static class, or initializers of the interface fields, in textual order, as if they were a single block.
In other words, this code is executed:
ref = new Test(); System.out.println("In Static Block of Test");
This first line creates an instance ... that requires starting the instance initializer. All initialization of this instance occurs before the control returns to the initialization part of the type - that is, before the line from the static initializer is started.
If you move the field declaration after the static initializer, you will see opposite results.
source share