java loading order example: chaos

package com.qcby.classTest;

public class InitializeDemo {
    
    
    private static int k = 1;
    private static InitializeDemo t1 = new InitializeDemo("t1");
    private static InitializeDemo t2 = new InitializeDemo("t2");
    private static int i = print("i");
    static {
    
    
        print("静态块");
        n=100;
    }
    private static int n = 99;
    
    {
    
    
        print("构造块");
        j=100;

    }
    public InitializeDemo(String str) {
    
    
        System.out.println((k++) + ":" + str + "   i=" + i + "    n=" + n);
        ++i;
        ++n;
    }
    private int j = print("j");
    public static int print(String str) {
    
    
        System.out.println((k++) + ":" + str + "   i=" + i + "    n=" + n);
        ++n;
        return ++i;
    }

    
    public static void main(String[] args) {
    
    
    	InitializeDemo  n =new InitializeDemo("初始化");
	}
  
}

Operation result:
Insert picture description here
Analysis:
When creating this type of object in the main method, first load the static part, execute it from top to bottom, first load k=1, then load t1 and t2, t1 and t2new a new object, respectively Execute the building block and member variable part and the construction method, load i after the execution, then load the static block, then load the object building block created in the main method, and then load j and the construction method

Guess you like

Origin blog.csdn.net/Walker7143/article/details/106036443