1. First, a netizen's blog post raises the topic:
Address: http://blog.csdn.net/tan605963296/article/details/5562928
Java classes are used to start loading. Before the constructor of each class is called, each domain (member variable) of the class must be initialized by the compiler first, then it is the turn of the constructor to do the work. However, Java Initialization is inert, And the Java compiler ensures that each variable is initialized before use. Therefore, Java only requiresProgramThe member can give a valid value to the class member in the appropriate place.
When you execute the Java compiler, the compiler looks for the specified class. When it finds the class, it first needs to initialize the static variable, because these Members can run without instances. Then, initialize the class members in sequence according to the declared order of the variables. After the initialization is complete, the constructor is called for itself. After all the operations are completed, the method can be called. However, the main class is special. The main method will be called after static variable initialization before the constructor. This is due to Java's inert initialization, before calling the main method, you cannot know whether there is an instance of the main class.
Why is the main class special? It can be understood as follows: the main method of public void main (string [] ARGs) is static and can be regarded as a static member, so it is called after static variable initialization.
Public class initialtest1 {int A; int B; static int C; {A =-1; B = 1;} // This is called initialization.CodeBlock static {c = 100;} // static members have exclusive static initialization code block initialtest () {syetem. Out. println ("initialtest creat now! ")} // This is indeed different from C ++, and the permission is greatly reduced}
2. If you want to run the above program, the JVM will not find the entry, but the following program will be executed:
Package com. Puzzle. Initialization; public class initialtest {static {system. Out. println ("initialization block");} public static void main (string [] ARGs ){}}
Therefore, the main method only provides an entry to some extent. With this entry, the program can run even if there is nothing in the main method.