10 分析类的初始化


package annotate;
// 分析类的初始化,取决于是否实例化对象?
public class Test10 {
    static {
        System.out.println("main被初始化");
    }
    public static void main(String[] args) throws ClassNotFoundException {
        // 被初始化
//        Class.forName("annotate.Son");  // 1 反射
//        new Son();    // 2  主动引用

        // 不被初始化
       // System.out.println(Son.a);  // 子类不被加载
       // Son[] sons = new Son[5];  // 数组
        System.out.println(Son.B);  // 常量

    }
}

class Father{
    static int a=1;
    static {
        System.out.println("父类被初始化");
    }
}

class Son extends Father{
    static int b=2;
    static final int B=3;
    static {
        System.out.println("子类被初始化】");
    }
}
posted @ 2023-09-13 16:13  被占用的小海海  阅读(6)  评论(0)    收藏  举报