浅谈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
- 浅谈Java类加载顺序
- 浅谈Java类加载
- Java类加载顺序
- java类加载顺序
- Java类加载顺序
- Java 类加载顺序
- java 类加载顺序
- java类加载顺序
- Java类加载顺序
- java类加载顺序
- Java类加载顺序
- java类加载顺序
- Java类加载顺序
- Java类加载顺序
- java 类加载顺序
- java 类加载顺序
- Java类加载顺序
- 浅谈Java中父类与子类的加载顺序
- windows7下启动mysql服务名无效
- 从零开始搭建微信硬件开发环境全过程——1小时掌握微信硬件开发流程
- redis索引的设计
- Android 内建Theme的使用
- centos6安装配置freeradius和mysql
- 浅谈Java类加载顺序
- 事务的隔离级别
- nth-child(n)选择器与:eq(index)选择器
- 华为机试——字符串压缩
- SVN Previous operation has not finished; run 'cleanup' if it was interrupted
- 清除浮动的几种常用方法
- (转)续上文,Unity3D面试ABC
- 大牛博客
- Perl脚本-为接口定义添加接口id