这是一些简单的Java代码。
class Test {
public static void main(final String[] args) {
TestClass c = new TestClass();
System.out.println(c.x);
}
}
class TestClass {
{
x = 2;
}
int x = 1;
}
我得到答案1.为什么?是否没有用于初始化的构造函数?
将TestClass编译为与此等效:
class TestClass {
{
this.x = 2;
}
int x;
{
this.x = 1;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句