Deep understanding of the JVM execution order __java Java class instantiation

Source: Internet
Author: User
Preface

About the JVM class loader we often ask in the interview that if the relevant concepts are not clear, you can refer to my in-depth understanding of the virtual machine such as loaders. This article is intended to demonstrate the sequence of JVM execution. Sample Code

Package Com.jdk.learn;
 /** * Created by Ricky on 2017/8/25. * * Class loader load Order Elegant * * */public class Classloadertest {public static void main (string[] args) {son sons=n
    ew son ();
    } class parent{private static int a=1;
    private static int B;
    private int c=initc ();
        static {b=1;
        System.out.println ("1. Parent class static code block: Assignment B successful");
    System.out.println ("1. Parent class static code block: Value of a" +a);
        int INITC () {System.out.println ("3. Parent class member variable assignment:---> C value" +c);
        this.c=12;
        System.out.println ("3. Parent class member variable assignment:---> C value" +c);
    return C;
        The public parent () {System.out.println ("4. Parent class constructor starts execution---> A:" +a+ ", B:" +b); "
    System.out.println ("4. The parent class constructs a way to execute---> C:" +c);
    } class son extends parent{private static int sa=1;
    private static int sb;
    private int sc=initc2 ();
        static {sb=1;
        System.out.println ("2. Subclass static code block: Assignment SB success");
    System.out.println ("2. Subclass static code block: SA value" +sa);
    }int Initc2 () {System.out.println ("5. Subclass member Variable Assignment--->:SC value" +SC);
        this.sc=12;
    return SC;
        Public son () {System.out.println ("6. Subclass constructor started execution---> SA:" +sa+ ", SB:" +SB);
    System.out.println ("6. Sub-class construction mode begins to execute---> SC:" +SC);
 }
}
Execution Results
1. Parent class static code block: Assignment B succeeded
 1. Parent class static code block: A's value 1
 2. Subclass Static code block: Assignment SB succeeded
 2. Subclass Static code block: SA value 1
 3. Parent class member variable assignment:---> C value 0
 3. Parent class member Variable assignment:---> C values
 4. The parent class is constructed to execute---> A:1,b:1
 4. The parent class constructor begins execution---> C:12
 5. Subclass member Variable Assignment--->:SC value 0
 6. The subclass constructs the way to begin to execute---> sa:1,sb:1
 6. The subclass constructs the way to begin to execute---> sc:12
Analysis

What did the JVM do when we were doing the new instantiation?
If you have seen a deep understanding of the JVM, it should be clear that when we instantiate using the New keyword, we will do the following: loading
This phase loads the byte code information of the class to a memory connection
This phase is validated, the default value is assigned, and the symbol reference is referred to directly

Class
Assigning values to members, and so on

Use
Operate on instances such as sons.tostring ()

For our example, we only focus on the connection and initialization phases.
In the connection phase we assign a default value of 0 to the class structure (the member variable (first), method (after), and so on) (not the writing order), and the object is null.

When initialized, the object is assigned a value, such as C execution INITC () for assignment. last

Author: Ricky

Exchange Group: 244930845

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.