JDK1.7 vs JDK1.6内部类继承差异
JDK 1.7 vs JDK 1.6 inner classes inheritance difference
我正在解决一些 Java 难题,偶然发现了这个难题:
public class Outer {
class Inner1 extends Outer {}
class Inner2 extends Inner1 {}
}
在使用 javac 1.6.0_45
编译此代码时,我得到了预期的错误:
Outer.java:8: cannot reference this before supertype constructor has been called
class Inner2 extends Inner1 {}
^
这是因为编译器为 Inner2
class 生成了默认构造函数,代码类似,这解释了上面的错误:
Inner2 () {
this.super();
}
现在很明显,因为在 Java 1.6.0_45、JLS 8.8.7.1(我猜)中你真的不能这样做:
An explicit constructor invocation statement in a constructor body may
not refer to any instance variables or instance methods declared in
this class or any superclass, or use this or super in any expression;
otherwise, a compile-time error occurs.
参见 (accepted answer in Odd situation for "cannot reference this before supertype constructor has been called")
但是如果我尝试用 javac 1.7.0_79
编译它 - 没问题!
问题来了 - 在 Java 1.7 中更改了什么,这段代码现在是正确的?
提前致谢!
我怀疑这与在 java 1.7 中添加的 invoke dynamic 有关,以便为 java 8.
中的 lambda 做准备
似乎在 Java 错误跟踪器上讨论了与错误 JDK-6708938: Synthetic super-constructor call should never use 'this' as a qualifier 相同的问题。
此外,我认为您最好看看上一个的其他相关问题,例如 JDK-4903103: Can't compile sub类 内部 类
.
注意这两个错误的修复版本。
结果见 Maintenance Review of JSR 901 (Java Language Specification) for Java SE 7。
来自The Java Language Specification Third Edition
Otherwise, S
is an inner member class (§8.5). It is a compile-time
error if S
is not a member of a lexically enclosing class, or of a
superclass or superinterface thereof. Let O
be the innermost lexically
enclosing class of which S
is a member, and let n be an integer such
that O
is the n th lexically enclosing class of C
. The immediately
enclosing instance of i
with respect to S
is the n th lexically
enclosing instance of this.
以及来自 Java SE 7 的 JSR 901(Java 语言规范)的维护审查(完整版,第 242 页,蓝色文本)或 The Java Language Specification, Java SE 7 Edition 中的相同内容(只是在第 8.8.8 节之前)
Otherwise, S is an inner member class (§8.5).
Let O be the innermost lexically enclosing class of S, and let n be an integer such that O is
the n'th lexically enclosing class of C.
The immediately enclosing
instance of i with respect to S is the n'th lexically enclosing
instance of this.
所以你可以看到编译时错误的部分已经消失了。
我正在解决一些 Java 难题,偶然发现了这个难题:
public class Outer {
class Inner1 extends Outer {}
class Inner2 extends Inner1 {}
}
在使用 javac 1.6.0_45
编译此代码时,我得到了预期的错误:
Outer.java:8: cannot reference this before supertype constructor has been called
class Inner2 extends Inner1 {}
^
这是因为编译器为 Inner2
class 生成了默认构造函数,代码类似,这解释了上面的错误:
Inner2 () {
this.super();
}
现在很明显,因为在 Java 1.6.0_45、JLS 8.8.7.1(我猜)中你真的不能这样做:
An explicit constructor invocation statement in a constructor body may not refer to any instance variables or instance methods declared in this class or any superclass, or use this or super in any expression; otherwise, a compile-time error occurs.
参见 (accepted answer in Odd situation for "cannot reference this before supertype constructor has been called")
但是如果我尝试用 javac 1.7.0_79
编译它 - 没问题!
问题来了 - 在 Java 1.7 中更改了什么,这段代码现在是正确的?
提前致谢!
我怀疑这与在 java 1.7 中添加的 invoke dynamic 有关,以便为 java 8.
中的 lambda 做准备似乎在 Java 错误跟踪器上讨论了与错误 JDK-6708938: Synthetic super-constructor call should never use 'this' as a qualifier 相同的问题。
此外,我认为您最好看看上一个的其他相关问题,例如 JDK-4903103: Can't compile sub类 内部 类 .
注意这两个错误的修复版本。
结果见 Maintenance Review of JSR 901 (Java Language Specification) for Java SE 7。
来自The Java Language Specification Third Edition
Otherwise,
S
is an inner member class (§8.5). It is a compile-time error ifS
is not a member of a lexically enclosing class, or of a superclass or superinterface thereof. LetO
be the innermost lexically enclosing class of whichS
is a member, and let n be an integer such thatO
is the n th lexically enclosing class ofC
. The immediately enclosing instance ofi
with respect toS
is the n th lexically enclosing instance of this.
以及来自 Java SE 7 的 JSR 901(Java 语言规范)的维护审查(完整版,第 242 页,蓝色文本)或 The Java Language Specification, Java SE 7 Edition 中的相同内容(只是在第 8.8.8 节之前)
Otherwise, S is an inner member class (§8.5).
Let O be the innermost lexically enclosing class of S, and let n be an integer such that O is the n'th lexically enclosing class of C.
The immediately enclosing instance of i with respect to S is the n'th lexically enclosing instance of this.
所以你可以看到编译时错误的部分已经消失了。