浅谈Java类加载顺序

之前一直搞错,这里总结一下java类加载顺序
一个类中主要有如下成员:
普通对象变量,静态对象变量,静态方法,普通代码块,静态代码块,普通方法。

总体规则:
静态在非静态之前初始化(无论父类还是子类)
先初始化父类,再初始化子类,子类只有静态初始在父类其他非静态初始之前,其他等父类初始化完毕,再初始化自己
静态对象变量,静态代码块初始化顺序看具体代码顺序
普通对象变量,普通代码块初始化顺序看具体代码顺序
构造函数在最后,无论书写顺序
静态方法,普通方法调用时才初始化
销毁时,先销毁子类,再销毁父类
静态变量,静态代码块只初始化一次

例子:

提供测试的Name.java

public class Name {
    public Name(String value) {
        System.out.println(value);
    }
}

父类Animal.java

public class Animal {

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

    public Animal() {
        System.out.println("father: constructor");
    }

    Name n1 = new Name("father: variable");

    {
        System.out.println("father: code block");
    }

    static Name n2 = new Name("father: static variable");

}

子类Bird.java

public class Animal {

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

    public Animal() {
        System.out.println("father: constructor");
    }

    Name n1 = new Name("father: variable");

    {
        System.out.println("father: code block");
    }

    static Name n2 = new Name("father: static variable");

}

运行结果:

father: static code block
father: static variable
child: static variable
child: static code block
father: variable
father: code block
father: constructor
child: code block
child: variable
child: constructor

子类初始化时候,默认寻找父类不带参数的构造函数,如果找不到,那就说明父类提供了一个带参的构造函数,那么子类必须用super关键字显示调用,而且必须放在自己构造函数第一行。而且此时只用带参的构造函数初始化。
例子:修改Animal.java

public class Animal {

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

    public Animal() {
        System.out.println("father: constructor");
    }

    public Animal(int i) {
        System.out.println("father: constructor parameter");
    }

    Name n1 = new Name("father: variable");

    {
        System.out.println("father: code block");
    }

    static Name n2 = new Name("father: static variable");

}

修改Bird.java

public class Bird extends Animal {
    static Name n4 = new Name("child: static variable");

    {
        System.out.println("child: code block");
    }

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

    Bird() {
        super(1);
        System.out.println("child: constructor");
    }

    Name n3 = new Name("child: variable");

    public static void main(String[] args) {
        new Bird();
    }
}

执行结果:

father: static code block
father: static variable
child: static variable
child: static code block
father: variable
father: code block
father: constructor parameter
child: code block
child: variable
child: constructor

注意:
如果我将代码main函数修改为如下这个样子,那么输出结果是什么呢?

public static void main(String[] args) {
        System.out.println("before main");
        new Bird();
    }

执行结果:

father: static code block
father: static variable
child: static variable
child: static code block
before main
father: variable
father: code block
father: constructor parameter
child: code block
child: variable
child: constructor

为什么呢?
执行main函数之前,java先加载这个类,static是类变量,因此需要先执行。如果我们在另外一个函数里面写上述main函数,那么before main就打印了,请看:
Client.java

public class Client {
    public static void main(String[] args) {
        System.out.println("before main");
        new Bird();
    }
}

执行结果:

before main
father: static code block
father: static variable
child: static variable
child: static code block
father: variable
father: code block
father: constructor parameter
child: code block
child: variable
child: constructor

再测试一下,静态变量只初始化一次
Client.java

public class Client {
    public static void main(String[] args) {
        System.out.println("before main");
        new Bird();
        System.out.println("second initial");
        new Bird();
    }
}

执行结果:

before main
father: static code block
father: static variable
child: static variable
child: static code block
father: variable
father: code block
father: constructor parameter
child: code block
child: variable
child: constructor
second initial
father: variable
father: code block
father: constructor parameter
child: code block
child: variable
child: constructor
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值