Such as:
public class innerclassdemo{
int x;
Class a{
static int a = 0;//is not legal to write this way.
static final int b=0;//It's legal to write this.
}
}
Java class loading order, first load the class, execute the static variable initialization, and then execute the object creation, if we want to execute the code in the variable int a initialization, then we must first execute loading the external class, then load the inner class, and finally initialize the static variable A, the problem is the loading of the inner class, We can think of the inner class as a non-static member of the outer class, its initialization must be done after the outer class object is created, the internal class must be completed after the instance, the Java virtual machine requires that all static variables must be completed before the object is created, which creates a contradiction.
(a bit around, hehe)
And the Java constant in the memory of the constant pool, its mechanism and variables are different, at compile time, load constants do not need to load the class, so there is no such contradiction.
Why can't a static inner class have static-decorated properties, but can have constants?