java显式构造函数_Java显式构造函数调用和实例初始化程序

编辑:事实证明,JLS毕竟是准确的,虽然它很难阅读.这一切都在

section 12.5中详述:

Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure:

Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.

If this constructor begins with an explicit constructor invocation (§8.8.7.1) of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.

This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.

Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5.

Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.

注意突出显示的部分 – 链接的构造函数被执行,然后我们跳过步骤4,它将执行实例初始化程序.

实际情况是,实例和字段初始化程序只执行一次,您可以从输出中判断出来.

非正式地,我认为将程序描述为:

>将构造函数链接在同一个类(this(…))中,直到到达不以此为开头的构造函数体.

>执行适当的超级构造函数

>执行实例变量初始值设定项和实例初始值设定项

>执行“最里面”构造函数的主体

>继续弹出构造函数体的堆栈,直到最终得到“entry”构造函数

Is that MEANS use this() within subclass’s constructor will implicit remove the default call to the no-argument constructor of the superclass?

是.在类的构造函数链中的某处,您可以保证最终得到一个隐式或显式调用super的构造函数.这是唯一被调用的超类构造函数.

编辑:请注意,您引用的教程明显不正确.

样本类:

public class Test {

{

System.out.println("Foo");

}

public Test() {

}

public Test(int i)  {

this();

}

}

javap -c的输出:

public class Test {

public Test();

Code:

0: aload_0

1: invokespecial #1 // Method java/lang/Object."": ()V

4: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;

7: ldc #3 // String Foo

9: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V

12: return

public Test(int);

Code:

0: aload_0

1: invokespecial #5 // Method "":()V

4: return

}

如您所见,Test(int)的构造函数没有编译到其中的实例构造函数的代码.

基本上,只有直接调用超类构造函数的构造函数才会将实例初始化代码复制到它们中.所有其他构造函数最终将导致实例初始化程序代码通过调用超类构造函数的构造函数执行,当然.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值