static

public class staticTest {
    
    Person person = new Person("Test");
    static {
        System.out.println("tset static");
    }
    
    public staticTest() {
        System.out.println("test constructor");
    }
    
    public static void main(String[] args) {
        new Myclass();
    }
}

class Person{
    
    static {
        System.out.println("person static");
    }
    
    public Person(String str) {
        System.out.println("person " + str);
    }
}

class Myclass extends staticTest{
    Person person = new Person("Mycalss");
    static {
        System.out.println("myclass static");
    }
    
    public Myclass() {
        System.out.println("myclass contructor");
    }
}

输出结果:

tset static
myclass static
person static
person Test
test constructor
person Mycalss
myclass contructor

解释:

类加载过程:加载,验证,准备,解析,初始化(类构造器)

加载(父类-子类),初始化成员变量,初始化执行构造器

你可能感兴趣的:(static)