Execution order of the static block, a static method, constructor

Original link: https://blog.csdn.net/qq_25615395/article/details/78294985

Reiko:
Package Io.Renren;

/**
 * Created by root on 9/29/17.
 */
public class StaticTest {


    static int age = 1;
    int age1 = 2;

    {static
        System.out.println ( "this is a static block");
    }

    {
        System.out.println ( "This is a normal block" AGE1 +);
    }

    StaticTest public () {
        System.out.println ( "This is the constructor");
    }

    static void Show public () {
        System.out.println ( "This is a static method");
    }

    Fun void public () {
        System.out.println ( "This is the conventional method");
    }

    public static void main(String[] args) {
        //System.out.println(age);
        StaticTest t = new StaticTest();
        StaticTest.show();
        t.fun();
        /*System.out.println(t.age1);*/

        String a = Boolean.toString(true);
        System.out.println(a);
    }

}


Results of the:


Initialization sequence Java
class variables (static variables), instance variables (non-static variable), a static code block, code block non-static initialization timing modified by the static keyword (such as: class variable [variable static], static code block) is class is initialized before being initialized create an instance, and are sequentially executed in the order from top to bottom;

No modification of the static keyword (eg: instance variables [non-static variable], 
non-static code blocks) actually initialization is extracted to class constructor is executed, but priority than class constructor code block to perform, which is also performed sequentially from top to bottom.

In one example, when the new B must first be loaded classes. (Java class will only be charged when the class loader calls created using the New)
during loading class the parent class is first loaded with A, then B subclass loading
after loading the parent class A, the static operation is completed (including static code and variables, which are the same level, in order of appearance in accordance with the initialization code)
after loading sub classes B, the static operation completion

Class loading is completed, a instantiated 

1. When instantiating subunit Classes B, must first instantiated parent class A2, an instance of the parent class A, the first member of the instantiated (non-static code) 
2. A parent class constructor is 
a member of Example 3. subclass B of (non-static code) 
constructor 4. subclass of B 

Initialize the static code parent -> child class to initialize static code -> parent class non-static initialization code -> parent class constructor initializes -> setup sub-class of non-static code -> subclass constructor initializes

 

 

to sum up:

Normal order of execution: a static class initialization code -> creates an instance of the class - based non initialize static code >> -> class constructor initializes

Sequentially performed in the hierarchy, B inherits A

Initialize static code of the parent class -> initialize the subclass of static code -> create an instance of the class - >> initialize the parent class's non-static code -> initialize the parent class constructor -> initialize the subclass of non-static code -> Initialize subclass constructors

Static code execution occurs during loading class, Location: Java class loader in

Performing a non-static code occurs during instantiation of the class, code execution in preference to the constructor. Location: class constructor


————————————————

 


Disclaimer: This article is the original article CSDN bloggers "Chen Ping Village Huangshan hero", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/qq_25615395/article/details/78294985

Guess you like

Origin blog.csdn.net/Daniel__Wu/article/details/102768513