有些人可能会发现它类似于 SO 问题 Will Java Final variables have default values?但是该答案并不能完全解决这个问题,因为该问题不会直接在实例初始化程序 block 中打印 x 的值。
当我尝试直接在实例初始化程序 block 内打印 x 时出现问题,同时在 block 结束之前为 x 分配了一个值:
案例一
class HelloWorld {
final int x;
{
System.out.println(x);
x = 7;
System.out.println(x);
}
HelloWorld() {
System.out.println("hi");
}
public static void main(String[] args) {
HelloWorld t = new HelloWorld();
}
}
这会产生一个编译时错误,指出变量 x 可能尚未初始化。
$ javac HelloWorld.java
HelloWorld.java:6: error: variable x might not have been initialized
System.out.println(x);
^
1 error
案例 2
我不是直接打印,而是调用一个函数来打印:
class HelloWorld {
final int x;
{
printX();
x = 7;
printX();
}
HelloWorld() {
System.out.println("hi");
}
void printX() {
System.out.println(x);
}
public static void main(String[] args) {
HelloWorld t = new HelloWorld();
}
}
这会正确编译并给出输出
0
7
hi
这两种情况的概念区别是什么?
最佳答案
在 JLS 中,§8.3.3. Forward References During Field Initialization ,它表示在以下情况下会出现编译时错误:
Use of instance variables whose declarations appear textually after the use is sometimes restricted, even though these instance variables are in scope. Specifically, it is a compile-time error if all of the following are true:
The declaration of an instance variable in a class or interface C appears textually after a use of the instance variable;
The use is a simple name in either an instance variable initializer of C or an instance initializer of C;
The use is not on the left hand side of an assignment;
C is the innermost class or interface enclosing the use.
以下规则附带几个示例,其中最接近您的是这个:
class Z {
static int peek() { return j; }
static int i = peek();
static int j = 1;
}
class Test {
public static void main(String[] args) {
System.out.println(Z.i);
}
}
通过方法访问[静态或实例变量]不会以这种方式检查,所以上面的代码产生输出0
,因为的变量初始化器i
使用类方法 peek()
在 j
被其变量初始化程序初始化之前访问变量 j
的值,此时它仍然具有其默认值( §4.12.5 Initial Values of Variables )。
因此,总而言之,您的第二个示例编译并执行良好,因为编译器不会检查 x
变量是否在您调用 printX()
时已经初始化,并且当 printX()
实际发生在运行时,x
变量将被赋予其默认值 (0
)。
关于java - 为什么尝试打印未初始化的变量并不总是导致错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33995384/