Java类的加载顺序

/**
 * @author yum
 * @date 2019/11/19
 */
public class Test {

    static class Father {
        static Hi hi1 = new Hi("father hi1");

        Hi hi2 = new Hi("father hi2");

        static {
            System.out.println("father static");
        }

        {
            System.out.println("father non-static");
        }

        public Father() {
            System.out.println("father init");
        }
    }


    static class Child extends Father {
        static Hi hi1 = new Hi("child hi1");

        Hi hi2 = new Hi("child hi2");

        static {
            System.out.println("child static");
        }

        {
            System.out.println("child non-static");
        }

        public Child() {
            System.out.println("child init");
        }
    }

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

    public static void main(String[] args) {
        System.out.println("初次 new Child:");
        Child child = new Child();

        System.out.println("第二次 new Child:");
        child = new Child();
    }

}

执行结果

初次 new Child:
Hi father hi1
father static
Hi child hi1
child static
Hi father hi2
father non-static
father init
Hi child hi2
child non-static
child init
第二次 new Child:
Hi father hi2
father non-static
father init
Hi child hi2
child non-static
child init

加载顺序为
父类静态属性(成员变量) > 父类静态代码块 > 子类静态属性 > 子类静态代码块 > 父类非静态属性 > 父类非静态代码块 > 父类构造器 > 子类非静态属性 > 子类非静态代码块 > 子类构造器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值