public class A{ static int a = 1111; static { a = a-- - --a; } { a = a++ + ++a; } public static void main(String[] args) { System.out.println(a); } } Ask what the program will display. It is clear to me that if we have not created an instance of the class, then the class-level variables (static) will be initialized, and the static block will also be executed, even if an instance of the class is not created. And the answer is here 2. Because at the time of the call, only the static initialization block was executed. But my question is different. I thought there would be an NPE exception, since the variable does not exist yet. I thought so because I believed that the public static void main(String[] args) entry point is executed first, and then in order of initialization passes.
So at what point the static field and the stat were initialized. class Π block?
mainmethod is accessed by the virtual machine when we start the program, specifying the class as an argument to it. If a class is not used by anyone, then it is not initialized. The order of the directory does not matter. - m. vokhm pm