The initialization sequence of the--java class for the JVM series learning

Source: Internet
Author: User

This article mainly demonstrates the initialization order of Java classes

Order of initialization

For static variables, static initialization blocks, variables, initialization blocks, constructors, they are initialized sequentially (static variables, static initialization blocks) > (variables, initialization blocks) > constructors.

Instance Code
public class Initialordertest {/        * static variable */public    static String Staticfield = "static variable";        /* variable */public    String field = "Variable";        /* Static initialization block *    /static {        System.out.println (Staticfield);        SYSTEM.OUT.PRINTLN ("Static initialization Block");    }        /* Initialize block *        /{System.out.println (field);        System.out.println ("initialization block");    }        /* Constructor */public    initialordertest ()    {        System.out.println ("constructor");    }    public static void Main (string[] args)    {        new Initialordertest ();}    }

  

Output

Running the above code, we will get the following output result:

    1. static variables

    2. Static initialization blocks

    3. Variable

    4. Initialize block

    5. Constructors

Instance Code for inherited cases
Class Parent {/* static variable */public static String P_staticfield = "Parent class-static variable";    /* variable */public String P_field = "Parent class-variable";    protected int i = 9;        protected int j = 0;        /* Static initialization block */static {System.out.println (P_staticfield);    System.out.println ("Parent class-static initialization block");        }/* Initialize block */{System.out.println (P_field);    System.out.println ("Parent class-Initialization block");        }/* constructor */public parent () {SYSTEM.OUT.PRINTLN ("parent class-constructor");        System.out.println ("i=" + i + ", j=" + j);    j = 20;         }}public class Subclass extends Parent {/* static variable */public static String S_staticfield = "Subclass-static variable";        /* variable */public String S_field = "subclass-variable";        /* Static initialization block */static {System.out.println (S_staticfield);    System.out.println ("Subclass-static initialization block");        }/* Initialize block */{System.out.println (S_field);    System.out.println ("Subclass-Initialization block"); }/* constructor */PUblic Subclass () {System.out.println ("subclass-Constructor");    System.out.println ("i=" + i + ", j=" + j);        }/* Program entry */public static void main (string[] args) {System.out.println ("Subclass Main Method");    New Subclass (); }}

  

Output

Parent class--Static variable
Parent class--static initialization block
Subclass--Static variables
Subclass--Static initialization block
Sub-class Main method
Parent class--variable
Parent class--initialization block
Parent class--constructor
I=9, j=0
Subclass--Variables
Subclass--Initialization block
Sub-class--constructor
I=9,j=20

The initialization of a static variable and a static initialization block for a subclass is done before the parent class's variables, initialization blocks, and constructors are initialized. Static variables, static initialization blocks, variables, initialization blocks are initialized in order, depending on the order in which they appear in the class.

Analysis
      • (1) Access Subclass.main (), (this is a static method), so the loader will look for the compiled subclass class code (that is, the Subclass.class file). During loading, the loader notices that it has a base class (that is, what extends means), and then it loads the base class. Whether you create a base class object or not, this process always happens. If the base class also has a base class, the second base class is also loaded, and so on.

      • (2) performs static initialization of the base class, then static initialization of the next derived class, and so on. This order is important because the static initialization of a derived class is likely to depend on the correct initialization of the base class member.

      • (3) When all the necessary classes have been loaded, start executing the main () method body and creating the object with new subclass ().

      • (4) class subclass There is a parent class, call the constructor of the parent class, and you can use super to specify which constructor to call. The construction and construction order of the base class is the same as for the derived class. First, the variables in the base class are initialized in literal order, and then the rest of the constructor of the base class is executed.

      • (5) The child class member data is initialized in the order in which they are declared, executing the remainder of the subclass constructor.

The initialization sequence of the--java class for the JVM series learning

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.