浅谈Java类加载顺序

来源:互联网 发布:上海关键词优化公司 编辑:程序博客网 时间:2024/05/20 02:21

之前一直搞错,这里总结一下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 blockfather: static variablechild: static variablechild: static code blockfather: variablefather: code blockfather: constructorchild: code blockchild: variablechild: 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 blockfather: static variablechild: static variablechild: static code blockfather: variablefather: code blockfather: constructor parameterchild: code blockchild: variablechild: constructor

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

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

执行结果:

father: static code blockfather: static variablechild: static variablechild: static code blockbefore mainfather: variablefather: code blockfather: constructor parameterchild: code blockchild: variablechild: 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 mainfather: static code blockfather: static variablechild: static variablechild: static code blockfather: variablefather: code blockfather: constructor parameterchild: code blockchild: variablechild: 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 mainfather: static code blockfather: static variablechild: static variablechild: static code blockfather: variablefather: code blockfather: constructor parameterchild: code blockchild: variablechild: constructorsecond initialfather: variablefather: code blockfather: constructor parameterchild: code blockchild: variablechild: constructor
0 0
原创粉丝点击