Loading time for Java classes in the JVM

Source: Internet
Author: User

The Java Virtual machine loads the data of the description class from the class file into memory, verifies and initializes the data, and finally forms the Java type that can be used directly by the virtual machine, which is the loading mechanism of the virtual machine.
Class starts from being loaded into the virtual machine's memory, and its entire lifecycle includes: Load (Loading), validate (verification), prepare (preparation), Parse (Resolution), Initialization (initialization), use (using), and unload (unloading) seven phases. Where the validation, preparation, and parsing of three parts are collectively referred to as the connection (linking), as shown below.


In these seven phases, the order of the five phases of loading, validating, preparing, initializing, and unloading is determined, and the loading process of the class must begin in this order, and the parsing phase is not necessarily, and in some cases it can start again after the initialization phase.

Each phase of a class's life cycle is usually cross-mixed, often invoking or activating another phase during one phase of execution.
The Java Virtual Machine specification does not have a mandatory constraint on when to start the class loading process, but for the initialization phase, the virtual machine specification strictly stipulates that there are only four cases where the class must be "initialized" (while the load, validate, prepare phase must start before this), and the four cases are categorized as follows:

1. When encountering the 4 bytecode directives of new, getstatic, putstatic, or invokestatic, if the class has not been initialized, it needs to trigger its initialization first. The most common Java code scenario for generating these 4 instructions is when instantiating an object using the New keyword, reading or setting a static field of a class (except for the static field that was final decorated, which has been placed in a constant pool by the compiler), and when invoking a static method of a class.
2. When you use the Java.lang.reflect package method to make a reflection call to a class, if the class has not been initialized, you need to trigger its initialization first.
3. When initializing a class, it is necessary to trigger the initialization of the parent class if it is found that its parent class has not yet been initialized.
4. When the virtual machine starts, the user needs to specify a main class to execute (the class that contains the main () method), and the virtual opportunity initializes the class first.
For these four triggering classes to initialize the scenario, in the Java Virtual Machine specification, the "only" and "only" four scenarios will be triggered. The behavior of these four scenarios is called an active reference to the class, except that all other ways of referencing the class do not trigger the initialization of the class, called the passive reference.

The following is a description of the passive reference by three instances:

Example 1:

Parent class Superclass.java public class Superclass {      static{           System.out.println ("Superclass init!");        }       public static int value = 123;  }  Subclass Subclass.java Public     class subclass extends Superclass {       static{System.out.println          ("Subclass init!");      }  }   main class Notinitialization.javapublic class Notinitialization {public      static void Main (string[] args) { C13/>system.out.println (Subclass.value);      }  }  
Output Result: Superclass init! 123  


From the results we can see that only the "superclass init!" is output. ", no output" subclass init! ”。 This is because for static fields, only classes that directly define the field are initialized, so when we refer to a static field defined in the parent class through a subclass, only the initialization of the parent class is triggered, not the initialization of the subclass.

Example 2:

Superclass[]   scs=new superclass[11];


As above, the initialization of a class is not triggered when an array of objects is initialized.

Example 3:

public class Constclass {   static {    system.out.printl ("const");}   public static final int age =123;  } public class notinitialization{public     static void Main (string[] args) {     System.out.println (constclass.age);}

The "const" does not appear at this time because the Notinitialization class has placed the variable age in the Constclass in the constant pool at compile time, and when accessed, the age can be taken directly, without triggering the initialization of constclass.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Loading time for Java classes in the JVM

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.