Will Java static code blocks be automatically executed when classes are loaded ?, Java static
Is the JAVA static code block automatically executed when the class is loaded?
I. first look at Java static methods and static variables.
Http://www.cnblogs.com/winterfells/p/7906078.html
Static code block
In the class, you can declare a piece of code as static. Such a program block is called a static initialization segment. The static code block is generally in the following format:
Static {statement sequence}
Public class staticBlock {// define a common main () method public static void main (String args []) {System. out. println ("This is main method. ") ;}// defines a static code block static {System. out. println ("This is static block. "); int stVar = 0; // This is a local variable, valid only in this block }}
After the program is compiled, use the java command to load the program and obtain the following output:
This is static block.
This is main method.
As shown in the preceding output,The static code block is executed even before the main method.. Tasks that can be completed in the main () method can be completed in static code blocks. However, there are still some differences between the two in execution. The main method is the entrance to the entire program startup, while the static code block is a process that exists in a class.
Is the static code block executed as long as the class is loaded?
2. Let's look at the JVM class loading process.
The loading stage consists of three basic actions:
In addition, if a class Loader encounters a missing or wrong class file during pre-loading, it must wait until the program takes the initiative to use the class for the first time to report the error.
The connection phase is divided into three parts:
When a class is actively used, Java virtual will initialize it. The following five situations are active usage:
1. In case of four bytecode commands, namely new, getstatic, putstatic, or invokestatic, class initialization is required if the class has not been initialized. These scenarios include: use the new keyword to instantiate an object, read or set static fields of a class, and call static methods of a class.
2. When using the java. lang. reflect package method for reflection call, if the class is not initialized, Initialization is required.
3. When initializing a class, it is found that its parent class has not been initialized. You need to initialize the parent class.
4. During JVM startup, the class specified by the user that contains the main method must be initialized first.
5. Support for dynamic languages in JDK1.7. When the result of parsing java. lang. invoke. MethodHandle is REF_getStatic, REF_putStatic, and REF_invokeStatic methods, the corresponding class is not initialized.
In fact, the execution of the static block occurs in the "initialization" phase. In the initialization phase, jvm initializes static variables and executes static blocks.
Let's take a look at several cases of executing static blocks:
1. The first new A () process will print ""; because this process includes Initialization
2. The first Class. the forName ("A") process prints ""; because this process is equivalent to Class. forName ("A", true, this. getClass (). getClassLoader ());
3. The first Class. forName ("A", false, this. getClass (). getClassLoader () process will not print "". Because false indicates that initialization is not performed during the course of loading the class. Static blocks are not executed without initialization.
Let's look at this example.
Class MyClass1 {static {// static block System. out. println ("static block") ;}} public class Main {Class [] classArray = {MyClass1.class // to reference this class, the Class must be loaded into the virtual machine, (reflection mechanism)}; public static void main (String [] args) {System. out. println ("hello word ");}}
Result:
hello world
So the conclusion is that static blocks are not executed without initialization.
Reference: Understanding Java Virtual Machine
Http://blog.csdn.net/jiese1990/article/details/40154329
Http://www.cnblogs.com/ivanfu/archive/2012/02/12/2347817.html