In Java, for a single class, all static initializers are executed in order (from top to bottom of the class) before the all constructors. The terms inside {} are added to the constructor; these are not static initializers (see http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html ).
So, instance 1 from Test1:
static { add(2); } ... static { add(4); }
Then in the static block there is the constructor Test1. It is called, so the instance element is initialized because the previous static initializers were called:
{ add(6); } ... { add(8); } ... { add(11); }
After that, the following operation is called in the constructor:
add(5);
Then we return in the first case to invoke the last static initiazer:
static { add(12); }
Finally, the class, if it is fully initialized, so the main method is called:
public static void main(String[] args) { System.out.println("Main method!"); add(10); }
So the conclusion is:
2 4 6 8 11 5 Constructor! 12 Main method! 10
fandango
source share