I wonder how java initialize these static variables. The code I can't understand is shown blow:
public class Main {
static int first=test();
static int second=2;
static int third=test();
public static void main(String[] args) {
System.out.println(first);
System.out.println(second);
System.out.println(third);
}
public static int test() {
return second;
}
}
The output of the simple code below is 0 2 2
If the compiler will ignore non-executable method automatically or the static variable is 0 before it is define?
Sorry for can't find accurate description to google it.
static{ System.out.print(first); }beforefirstis declared ,there comes with a error.Why? if statics is initialized before class is loaded , the code above should be valid.firstideone.com/hGqlX5