分析类初始化

什么时候会发生类初始化

image

package com.guo.reflection;

//测试类什么时候会初始化
public class Test06 {
    static {
        System.out.println("main类被加载");
    }

    public static void main(String[] args) throws ClassNotFoundException {

        //1.主动引用
        //Son son = new Son();

        //2.反射也会产生主动引用
        //Class.forName("com.guo.reflection.Son");


    //不会产生类的引用的方法
        //1.通过子类引用父类的静态变量,不会导致子类初始化
        //System.out.println(Son.b);

        //2.通过数组定义类引用,不会触发类的初始化
        //Son[] array = new Son[5];

        //3.引用常量不会触发类的初始化(常量在链接阶段就存入了调用类的常量池中了)
        System.out.println(Son.M);


    }

}

class Father{

    static int b = 2;

    static {
        System.out.println("父类被加载");
    }

}


class Son extends Father{
    static {
        System.out.println("子类被加载");
        m = 300;
    }
    static int m = 100;
    static final int M = 1;
}
posted @ 2026-04-05 16:04  果子同志  阅读(10)  评论(0)    收藏  举报