It was previously assumed that the static block was executed at class load, and today when validating spring initializing the context loader, it was discovered that the Bean's static block was not executed.
Java code:
1 Class a{
2 static{
3 System.out.println ("Static Block invoked!")
4 }
5}
So when did the static block run? If you understand the principle of the JVM, we know that the operation of a class is divided into the following steps:
- Loading
- Connection
- Initialization
The loading phase consists of three basic movements:
- Produces a binary data stream representing the type through the fully qualified name of the type
- This binary data stream is parsed as an internal data node within the method area.
- Creates an instance of the Java.lang.Class class that represents the type
In addition, if a class loader encounters a missing or incorrect class file when preloaded, it needs to wait until the program first actively uses the class to report an error.
The connection phase is divided into three parts:
- Verify that the type conforms to the semantics of the Java language, checks for binary compatibility between classes (such as final classes do not have subclasses, etc.), and also requires validation of symbolic references.
- Prepare, the Java Virtual machine allocates memory for the class variable and sets the default initial value.
- Parsing (optional), searching for the symbolic references of classes, interfaces, fields, and methods in a constant pool of types, replacing these symbolic references with direct reference procedures.
When a class is actively used, Java virtual initializes it, and the following six scenarios are active:
- When a new instance of a class is created (such as by new or reflected, cloned, deserialized, etc.)
- When you call a static method of a class
- When you use a static field for a class or interface
- When invoking some of the reflection methods in the Java API, such as methods in class classes, or methods of classes in Java.lang.reflect
- When a child class is initialized
- When the virtual machine launches a class that is identified as the startup class (that is, the class that contains the Main method)
The Java compiler collects all class-variable initialization statements and static initializers of types, putting them into a special method: Clinit.
In fact, the execution of the static block occurs during the initialization phase. During the initialization phase, the JVM mainly completes the initialization of static variables, static block execution, and so on.
Let's take a look at a few things that execute a static block:
1. The process of the first new A () will print ""; Because this process includes initialization
2, the first class.forname ("a") process will print "", because this process is equivalent to Class.forName ("a", True,this.getclass (). getClassLoader ());
3. The first class.forname ("A", False,this.getclass (). getClassLoader ()) process does not print "". Because false indicates the process of loading the class, it is not initialized. The static block is not executed without initialization.
Java Static code block execution time