In-depth understanding of Java Virtual Machine notes---class loading time

Source: Internet
Author: User
Tags array definition

Class starts from being loaded into the virtual machine memory, and the entire life cycle of the unloading is loaded (Loading), validated (verification), prepared (preparation), parsed (Resolution), initialized ( initialization), use (using), unloading load (unloading) seven stages. Where validation, preparation, parsing three phases collectively referred to as the connection (linking) phase, the sequence of these seven phases is as follows:

Load, verify, prepare, initialize, and unloading the order of the five phases is determined, the loading process of the class must begin in this order, and the parsing phase is not necessarily: in some cases, it can be started after the initialization phase, which is to support runtime bindings for the Java language. Note that it is written as a step-by-step "start" rather than a "do" or "complete" process, as these phases are usually mixed in a cross-section, usually in one phase of the procedure called or activated at another stage.
Under what circumstances do you need to start the first stage of the class's loading process: load. There is no mandatory constraint in the virtual machine specification. However, for the initialization phase, the virtual machine specification is strictly defined and there are only four cases where the class must be "initialized" (and loaded, validated, and the preparatory phase will naturally need to begin):
1. When encountering new,getstatic,putstatic or invokestatic 4 bytecode instructions, 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, when reading or setting a static field of a class, 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 human, it is necessary to trigger the initialization of its parent class if it finds that its parent 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 main class first.
For these four scenarios that trigger classes to initialize, a strong qualifier is used in the virtual machine specification: "Yes and only", and the behavior in these four scenarios is called an active reference to a class. In addition to all methods that reference a class, no initialization is triggered, called a passive reference. Here are three examples of passive references:

A. Referencing a static field in a parent class through a subclass does not initialize the child class

Package com.xtayfjpk.jvm.chapter7;/** * Passive use class field demonstrates one: * The parent class's static field is referenced by a subclass and does not cause subclasses to initialize * @author ZJ * */public class Superclass {PU Blic static int value = 123;static {System.out.println ("superclass Init");}}

Package Com.xtayfjpk.jvm.chapter7;public class Subclass extends Superclass {static {System.out.println ("Subclass Init" );}}

Package Com.xtayfjpk.jvm.chapter7;public class Notinitialization {/** * @param args */public static void main (string[] Arg s) {//Refers to a static field of the parent class through a subclass and does not cause subclasses to initialize SYSTEM.OUT.PRINTLN (Subclass.value);}}


After the above code is run, only "superclass init!" is output, not "subclass init!". For a static field, only the class that directly defines the field is initialized, so referencing a static field in the parent class through the subclass will only trigger the initialization of the parent class without triggering the subclass. As to whether to trigger the loading and validation of subclasses, it is not specified in the virtual machine specification, depending on the implementation of the virtual machine. For sun hotspot virtual machines, it is possible to see whether this action causes the child class to load (in fact subclass is loaded) through the-xx:+traceclassloading parameter.


B. Referencing a class by defining an array does not trigger initialization of this class
Package Com.xtayfjpk.jvm.chapter7;public class Notinitialization {/** * @param args */public static void main (string[] Arg s) {//The class is referenced by an array definition and does not trigger initialization of this class superclass[] SCA = new superclass[10];}}


after the above code is run (superclass reuse the previous example code), and there is no output "superclass init!", the description does not trigger the initialization phase of superclass. But this code triggers another named "[Lcom.xtayfjpk.jvm.chapter7.SuperClass;" Class initialization phase, for user code, this is not a valid class name, it is a virtual machine generated automatically, directly inherit from the Java.lang.Object subclass, the creation action is triggered by the bytecode directive NewArray.

C. Referencing constants in a constant pool without pin triggering to define initialization of a constant class
Package Com.xtayfjpk.jvm.chapter7;public class Constclass {public static final String HELLOWORLD = "Hello world"; static {S Ystem.out.println ("Constclass inited");}}

Package Com.xtayfjpk.jvm.chapter7;public class Notinitialization {/** * @param args */public static void main (string[] Arg s) {//constants are stored in the constant pool of the calling class at compile-time, and there is no direct reference to the class that defines the constant, which does not cause the class to initialize System.out.println (Constclass.helloworld);}}


After the code is run, there is no output "Constclass init!", because although the constant HelloWorld in the Constclass class is referenced in the Java source, the compile phase will "Hello World" for this constant value. stored in the constant pool of the Notinitialization class, the reference to the object constant Constclass.helloworld actually translates to the Notinitialization class's reference to its own constant pool. That is to say, in fact, Notinitialization class file does not have the constclass of the symbol reference entry, the two classes are translated into class after the absence of any connection.
The loading process of the interface is slightly different from the loading process of the class, there are some special instructions for the interface: The interface also has the initialization process, which is consistent with the class, the above code is the static statement block "static {}" to output the initialization information, and the interface can not have "static {}" statement block , the compiler will still generate the "<clinit>" class constructor for the interface, which initializes the member variables (also constants) defined in the interface. What really distinguishes between interfaces and classes is the third in the scenario where "there is only one" need to start the initialization stage: When a class is initialized, it requires that its parent class be initialized, but when an interface is initialized, it does not require its parent interface to be fully initialized, only when the parent interface is actually used ( The constants defined in the interface are initialized only if they are referenced.

In-depth understanding of Java Virtual Machine notes---class loading time

Related Article

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.