这无法编译(带有illegal forward reference
错误),正如人们所期望的:
class test {
int x = x + 42;
}
但这有效:
class test {
int x = this.x + 42;
}
这是怎么回事?在后一种情况下会分配什么?
发现并禁止所有访问太困难x
在 x 初始化期间。例如
int x = that().x; | int x = getX();
|
Test that(){ return this; } | int getX(){ return x; }
该规范停留在“通过简单名称访问”,并没有尝试变得更全面。
在另一节“明确分配”中,规范做了类似的事情。例如
public class Test
{
static final int y;
static final int z = y; // fail, y is not definitely assigned
static{ y = 1; }
}
public class Test
{
static final int y;
static final int z = Test.y; // pass... because it's not a simple name
static{ y = 1; }
}
有趣的是,“明确分配”特别提到this.x
相当于x
(或者,对于字段,是由此限定的字段的简单名称)
该条款也可以添加到 NPE 引用的部分中。
但最终,不可能在编译时分析某个字段的所有可能的使用/访问。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)