深入分析Java对象的建构顺序
作者:网络转载 发布时间:[ 2013/6/18 10:06:47 ] 推荐标签:
对于下面的代码,许多有经验的程序员都没能给出正确的答案。如果你能只看代码给出的答案和文章末尾出给出大答案一致,那么你已经掌握了Java对象的建构顺序,中间的分析可以不用看了。
/**
* 父类Foo,实现控制台输出
*
* @author youngto
* @since 2013-01-25
*/
class Foo {
private int index = 100;
//静态代码块
static {
System.out.println("Foo static");
}
//初始化代码块
{
System.out.println("Foo initialization");
}
public Foo() {
System.out.println("Foo constructor");
System.out.println(printIndex());
}
protected int printIndex() {
return index;
}
}
/**
* 子类Bar,实现控制台输出
*
* @author youngto
* @since 2013-01-25
*/
public class Bar extends Foo{
private int index = 100;
static Bar bar = new Bar();
//静态代码块
static{
System.out.println("Bar static");
}
//初始化代码块
{
System.out.println("Bar initialization");
}
public Bar() {
System.out.println("Bar constructor");
System.out.println(printIndex());
}
@Override
protected int printIndex() {
System.out.println(bar);
return index;
}
public static void main(String[] args) {
Foo foo = new Bar();
System.out.println(foo.printIndex());
foo = new Bar();
}
}
在对象建构过程中。为确保其正确性,以下事件一定会以固定顺序发生:
a、从heap之中分配内存,用以存放全部的 instance 变量以及这个对象连同其 superclasses的实现专属数据(implementation-specific data)。所谓「实现专属数据」包括指向“class and method data的指针。
b、 对象的Instance变量被初始化为其相应的缺省值。
c、调用most derived class(深层派生类)的构造函数(constructor)(注:事实上,构造函数被.class文件中的一个initialization method(初始化函数)替换了。Initialization method是名为<init>的特殊函数,由Java编译器安放在.class文件里。其中包含[构造函数代码]、[instance变量之初始化代码],以及[调用superclass Initialization method]之代码。)。构造函致做的第一件事是调用superclass的构造函数。这个程序一直反复持续到 java.lang.object构适函数被调用为止。一定要记住,java.lang.object是一切java对象的base class。
d、所有对象的静态代码块或静态字段先获得执行,优先级从父类开始。
e、在构造函数本体执行之前,所有 instance 变量的初值设定式(initializers)和初始化区(initialization blocks)先获得执行,然后才执行构造函数本体。于是base class的构造函数先执行,most derived class的构造函数后执行。这使得任何class的构造函数都能放心大胆地使用其任何superclass 的instance 变量。
相关推荐
更新发布
常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11系统性能测试及调优前期准备
2021/4/15 14:41:29国内比较好用的5款测试管理工具
2021/3/25 17:23:31