Understanding Java class loading timing and process from a face test

Source: Internet
Author: User
Tags array definition

Description: The content of this article is to read the "deep understanding of Java Virtual Machine: JVM advanced features and best practices" to add to the impression and understanding, then recorded the important content.

1 straight to the point

I used to see a Java face test, but I thought it was simple, but I ran the code, but the result was not what I imagined. The topics are as follows:

 class SingleTon { Private StaticSingleTon SingleTon =NewSingleTon (); Public Static intCount1; Public Static intCount2 =0;PrivateSingleTon () {count1++;  count2++; } Public StaticSingleTon getinstance () {returnSingleTon; }} Public  class Test {  Public Static voidMain (string[] args) {SingleTon SingleTon = singleton.getinstance (); System.out.println ("count1="+ singleton.count1); System.out.println ("count2="+ Singleton.count2); }}

Wrong answer

Count1=1

Count2=1

Correct answer

count1=1

count2=0

For God's horse? For God's horse? This should start with the Java class loading time.

Loading time for Class 2

Class starts from being loaded into the virtual machine's memory until it is unloaded, and its entire lifecycle includes the 7 phases of loading, validating, preparing, parsing, initializing, using, and uninstalling . wherein, the validation, preparation and resolution of these three parts are collectively referred to as the connection (linking) .


Where the order of the five stages of loading, validating, preparing, initializing, and unloading is determined, the loading process of the class must be "started" in this order (only the beginning, not the execution or the end, as these phases are usually cross-mixed, It is common to invoke or activate another phase during one phase of execution, while the parsing phase is not necessarily (it can be started after the initialization phase in some cases, in order to support runtime bindings for the Java language.)

3 When to start the initialization of a class

What happens when you start the first phase of the class loading process: "Load". The virtual machine specification does not impose constraints, this can be given to the virtual machine's specific implementation of the free grasp, but for the initialization phase of the virtual machine specification is strictly defined in the following cases, if the class is not initialized will initialize the class.

    1. To create an instance of a class
    2. To access static variables of a class (except for constants "static variables that are final rhetoric" cause: Constants a special variable, because the compiler treats them as values (value) rather than field (field). If your code uses a constant variable (constant variable), the compiler does not generate bytecode to load the value of the field from the object, but instead directly inserts the value into the byte code. This is a useful optimization, but if you need to change the value of the final field, every piece of code that uses that field needs to be recompiled.
    3. Accessing static methods of a class
    4. Reflection as ( class.forname ("My.xyz.Test") )
    5. When initializing a class, it is found that its parent class has not yet been initialized, then the initialization of the parent class is started
    6. When the virtual machine starts, the class that defines the main () method is initialized first.

The above situation is called a " proactive reference" to a class, except in this case, the initialization of the class is not triggered, which is called a "passive reference"

The loading process of an interface is slightly different from the loading process of a class. The static{} block cannot be used in an interface. When an interface is initialized, it does not require all of its parent interfaces to be initialized, only if it is actually used on the parent interface (for example, constants defined in the reference interface).

4 Passive Reference examples
    1. Subclasses call the static variables of the parent class, and the subclass is not initialized. Only the parent class is initialized: For static fields, only classes that directly define the field are initialized.
    2. Class is referenced by an array definition and does not trigger initialization of the class
    3. accesses a constant of a class and does not initialize the class
 class Superclass { Static{System.out.println ("Superclass Init"); } Public Static intValue =123;} class subclass extends superclass { Static{System.out.println ("Subclass Init"); }} Public  class Test {  Public Static voidMain (string[] args) {System.out.println (subclass.value);//Passive application 1subclass[] SCA =Newsubclass[Ten];//Passive reference 2}}
Program Run output Superclass init

123

The passive reference 1 and the passive reference 2 are demonstrated from the above input results

 class Constclass { Static{System.out.println ("Constclass Init"); } Public Static FinalString HELLOWORLD ="Hello World";} Public  class Test {  Public Static voidMain (string[] args) {System.out.println (Constclass.helloworld);//Call class constants}}
The program output Hello World proves the passive reference 3 from the above output result
Loading process for Class 55.1 Loading

The Loading stage is the first stage of the class Loading process, in which the virtual machine needs to complete the following three things:

1. Obtain a binary byte stream that defines this class by using the fully qualified name of a class.

2. Transform the static storage structure represented by this byte stream into the runtime data structure of the method area.

3. Generate a Java.lang.Class object representing this class in the Java heap as the access entry for the data in the method area.

The load phase can be done using the system-provided classloader, or it can be done by a user-defined ClassLoader. The load phase and part of the connection phase, such as a partial bytecode file format validation action, are interleaved, the loading phase is not complete, and the connection phase may have started.

5.2Verify

Validation is the first step in the connection phase, which is designed to ensure that the information contained in the byte stream of a class file conforms to the requirements of the current virtual machine and does not compromise the security of the virtual machine itself.

The Java language itself is a relatively safe language, and using Java encoding is not possible, such as accessing data outside the bounds of an array, transforming an object into a type that it does not implement, and, if so, the compiler will refuse to compile. However, the class file does not have to be compiled from Java source code, and can be used in any way, including writing directly with a hex editor such as UltraEdit. If a malicious "code" (byte stream) is written directly, and the virtual machine does not check when it loads the class, it can compromise the security of the virtual machine or program.

Depending on the virtual machine, the implementation of the class validation may be different, but the following four phases of validation are almost complete: file format validation, metadata validation, bytecode validation, and symbol reference validation.

1, file format verification, is to verify that the byte stream conforms to the class file format specification, and can be processed by the current version of the virtual machine. such as verifying that the magic number is 0xCAFEBABE, whether the main and minor version numbers are in the current virtual machine processing range, constant pool constants are not supported by the constant type ... The main purpose of this verification phase is to ensure that the input byte stream can be correctly parsed and stored in the method area, after this phase of validation, the byte stream will enter the memory of the method area to store, so the subsequent three verification phases are based on the storage structure of the method area.

2, metadata verification, is a byte code description of the information semantic analysis, to ensure that the information described in accordance with the requirements of the Java language specification. The validation may include whether the class has a parent class, whether the parent of the class inherits from a class that is not allowed to inherit, and if the class is not an abstract class, implements all the methods that are required to be implemented in its parent class or interface ...

3, bytecode verification, the main task is to carry out data flow and control flow analysis, to ensure that the method of the calibration class will not be harmful to the virtual machine security behavior. If the bytecode of a class method body is not verified by bytecode, it must be problematic, but if a method body passes the bytecode verification, it does not necessarily mean that it is safe.

4. Symbolic reference validation occurs when a virtual machine converts a symbolic reference to a direct reference, and this conversion action occurs in the "parsing phase." Verify that the permission specified by the string in the symbol reference specifies whether the corresponding class is found, whether there is a description of the methods and fields that conform to the method fields in a given class, and the accessibility of the classes, fields, and methods in the symbol reference (private, protected, public, Default) can be accessed by the current class

The validation phase is not necessarily a necessary stage for the class loading mechanism of a virtual machine. If all code validation that is running is secure, you can use the -xverify:none parameter to turn off most of the class validation measures to shorten the load time of the virtual machine class.

5.3 Preparation

The prep phase allocates memory for the static variables of the class and initializes them to the default values, which are allocated in the method area. The prepare phase does not allocate memory for instance variables in the class, and instance variables are allocated in the Java heap along with the objects as they are instantiated.

public static int value=123;//in the prep phase value has an initial values of 0. It will not change to 123 during the initialization phase.

5.4 Parsing

The parsing phase is the process by which a virtual machine replaces a symbolic reference within a constant pool with a direct reference.

Symbol reference (Symbolic Reference): a symbol reference is a set of symbols to describe the referenced target, the symbol can be any form of the literal, as long as the use can be used without ambiguity to locate the target. The symbolic reference is independent of the memory layout implemented by the virtual machine, and the referenced target is not necessarily loaded into memory.

Direct Reference: A direct reference can be a pointer to a target directly, a relative offset, or a handle that can be indirectly anchored to the target. The direct reference is related to the memory layout implemented by the virtual machine, and if there is a direct reference, the target of the reference must already exist in memory.

5.5 Initialization

Class initialization is the last step in the class loading process, and the rest of the action is completely dominated and controlled by the virtual machine, except in the load phase, where the user application can participate through the custom ClassLoader. In the initialization phase, the Java program code defined in the class is actually started.

The initialization phase is the process of executing the class constructor <clinit> () method. The <clinit> () method is generated by the compiler's automatic collection of assignment actions for all class variables in the class and statements in the static statement block (static{} block).

6 Topic Analysis

The above detailed introduction of the class loading time and class loading process, through the above theory to analyze the opening of this article to see the topic

 class SingleTon { Private StaticSingleTon SingleTon =NewSingleTon (); Public Static intCount1; Public Static intCount2 =0;PrivateSingleTon () {count1++;  count2++; } Public StaticSingleTon getinstance () {returnSingleTon; }} Public  class Test {  Public Static voidMain (string[] args) {SingleTon SingleTon = singleton.getinstance (); System.out.println ("count1="+ singleton.count1); System.out.println ("count2="+ Singleton.count2); }}

Analysis:

1:singleton SingleTon = Singleton.getinstance (); The SingleTon of the class called the static method of the class, triggering the initialization of the class
2: Class loads when the static variables of the class are allocated memory during the preparation process and initialize the default values Singleton=null count1=0,count2=0
3: class initialization, assigning values to static variables of a class and executing static code fast. SingleTon assignment to New SingleTon () calling class construction method
4: After calling the constructor of the class Count=1;count2=1
5: Continue assigning values to Count1 and Count2, at which point Count1 has no assignment, all count1 is 1, but Count2 performs assignment becomes 0

Understanding Java class loading timing and process from a face test

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.