A section in JLS can be found: §12.4.2.
Detailed Initialization Procedure:
9.Next, execute either the class variable initializers and static initializers of the class, or the field initializers of the interface,
in textual order, as though they were a single block, except that
final class variables and fields of interfaces whose values are
compile-time constants are initialized first
So the three static variable will be initialized one by one in textual order.
So
static A obj = new A();
//num1 = 1, num2 = 1;
static int num1;
//this is initilized first, see below.
static int num2=0;
//num1 = 1, num2 = 0;
If I change the order to:
static int num1;
static int num2=0;
static A obj = new A();
The result will be 1,1
.
Note that the static int num1;
is not a variable initializer because(§8.3.2):
If a field declarator contains a variable initializer, then it has the
semantics of an assignment (§15.26) to the declared variable, and: If
the declarator is for a class variable (that is, a static field), then
the variable initializer is evaluated and the assignment performed
exactly once, when the class is initialized
And this class variable is initialized when the class is created. This happens first(§4.12.5).
Every variable in a program must have a value before its value is
used: Each class variable, instance variable, or array component is
initialized with a default value when it is created (§15.9, §15.10):
For type byte, the default value is zero, that is, the value of
(byte)0. For type short, the default value is zero, that is, the value
of (short)0. For type int, the default value is zero, that is, 0. For
type long, the default value is zero, that is, 0L. For type float, the
default value is positive zero, that is, 0.0f. For type double, the
default value is positive zero, that is, 0.0d. For type char, the
default value is the null character, that is, '\u0000'. For type
boolean, the default value is false. For all reference types (§4.3),
the default value is null.