Reprinted from: http://chenzhou123520.iteye.com/blog/1597597
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 These 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 One parent class Superclass.java Java code Collection code Packagecom.chenzhou.classloading; /*** Classname:superclass <br/> * Function: Passive use class: Referencing a static field of a parent class through a subclass does not cause subclasses to initialize. <br/> * date:2012-7-18 Morning 09:37:06 <br/> *@authorChenzhou *@version * @sinceJDK 1.6 *@see */ Public classSuperclass {Static{System.out.println ("Superclass init!"); } Public Static intValue = 123; Subclass Subclass.javajava Code Collection Code Packagecom.chenzhou.classloading; Public classSubclassextendsSuperclass {Static{System.out.println ("Subclass Init!"); }} main class Notinitialization.javajava code collection code Packagecom.chenzhou.classloading; /*** classname:notinitialization <br/> * Function: Non-Active Use Class field demo. <br/> * date:2012-7-18 am 09:41:14 < ;br/> *@authorChenzhou *@version * @sinceJDK 1.6 *@see */ Public classnotinitialization { Public Static voidMain (string[] args) {System.out.println (subclass.value); }} output: TXT Code collection code superclass init! 123from 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 two parent class Superclass.java as the previous example main class Notinitialization.java Java code Collection code Packagecom.chenzhou.classloading; /*** classname:notinitialization <br/> * Function: Referencing a class through an array definition does not trigger initialization of this class. <br/> * date:2012-7-18 AM 0 9:41:14 <br/> *@authorChenzhou *@version * @sinceJDK 1.6 *@see */ Public classnotinitialization { Public Static voidMain (string[] args) {superclass[] SCS=NewSuperclass[10]; }} output is empty no output "superclass init! "The description does not trigger the initialization phase of the class Com.chenzhou.classloading.SuperClass, but this code triggers the initialization phase of the" [Lcom.chenzhou.classloading.SuperClass "class. This class is automatically generated by the virtual machine, which is triggered by NewArray. Example three constant class Constclass.java Java code Collection code Packagecom.chenzhou.classloading; /*** Classname:constclass <br/> * Function: Constants are stored in the constant pool of the calling class at compile time and are not inherently referenced directly to the class that defines the constants, so initialization of classes that define constants is not triggered. <br/> * Reason:todo ADD Reason. <br/> * date:2012-7-18 Morning 09:46:56 <br/> *@authorChenzhou *@version * @sinceJDK 1.6 *@see */ Public classConstclass {Static{System.out.println ("Constclass init!"); } Public Static FinalString HELLOWORLD = "Hello World"; } main class Notinitialization.javajava code collection code Packagecom.chenzhou.classloading; /*** classname:notinitialization <br/> * Function: Non-Active Utility Class field demo. <br/> * date:2012-7-18 am 09:41:14 &L T;br/> *@authorChenzhou *@version * @sinceJDK 1.6 *@see */ Public classnotinitialization { Public Static voidMain (string[] args) {System.out.println (Constclass.helloworld); }} output: Hello World
The example code above runs without output "superclass init! "This is because while the constant HelloWorld in the Constclass class is referenced in the Java source code, the value" Hello World "of this constant is stored in the constant pool of the Notinitialization class during the compilation phase. References to constant Constclass.helloworld are actually converted to the Notinitialization class's reference to its own constant pool. In fact, there is no sign reference entry for the Constclass class in the Notinitialization classes file.
The interface's loading process differs from class loading in the third of the four scenarios mentioned above, when a class requires its parent class to be initialized at initialization time, but an interface is initialized, it does not require its parent to complete initialization, only when the parent interface is actually used, such as a constant referencing the parent interface.
Java class loading time _4 active references violate class loading + The remaining passive references do not trigger the loading of the class