Java Static code block execution time

Source: Internet
Author: User

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:

    1. Loading
    2. Connection
    3. Initialization

The loading phase consists of three basic movements:

    1. Produces a binary data stream representing the type through the fully qualified name of the type
    2. This binary data stream is parsed as an internal data node within the method area.
    3. 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:

    1. 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.
    2. Prepare, the Java Virtual machine allocates memory for the class variable and sets the default initial value.
    3. 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:

    1. When a new instance of a class is created (such as by new or reflected, cloned, deserialized, etc.)
    2. When you call a static method of a class
    3. When you use a static field for a class or interface
    4. 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
    5. When a child class is initialized
    6. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.