为什么两个程序有前向引用错误而第三个没有? [英] Why do two programs have forward referencing errors while the third does not?
问题描述
以下内容无法编译,提供非法转发引用消息:
The following does not compile, giving an 'illegal forward reference' message:
class StaticInitialisation {
static
{
System.out.println("Test string is: " + testString);
}
private static String testString;
public static void main(String args[]) {
new StaticInitialisation();
}
}
但是,以下编译:
class InstanceInitialisation1 {
{
System.out.println("Test string is: " + this.testString);
}
private String testString;
public static void main(String args[]) {
new InstanceInitialisation1();
}
}
但以下不编译,给出'非法转发引用'消息:
But the following does not compile, giving an 'illegal forward reference' message:
class InstanceInitialisation2 {
private String testString1;
{
testString1 = testString2;
}
private String testString2;
public static void main(String args[]) {
new InstanceInitialisation2();
}
}
为什么StaticInitialisation和InstanceInitialisation2不能编译,而InstanceInitialisation1会这样做?
Why do StaticInitialisation and InstanceInitialisation2 not compile, while InstanceInitialisation1 does?
推荐答案
这部分由 8.3.3 :
使用类变量,其声明在使用后以文本形式出现有时受到限制,即使这些类变量在范围内(第6.3节)。具体来说,如果满足以下所有条件,则为编译时错误:
Use of class variables whose declarations appear textually after the use is sometimes restricted, even though these class variables are in scope (§6.3). Specifically, it is a compile-time error if all of the following are true:
-
声明类变量类或接口C在使用类变量后以文本方式显示;
The declaration of a class variable in a class or interface C appears textually after a use of the class variable;
在C的类变量初始化程序或静态初始化程序中使用是一个简单名称of C;
The use is a simple name in either a class variable initializer of C or a static initializer of C;
使用不在作业的左侧;
The use is not on the left hand side of an assignment;
C是封闭使用的最内层类或接口。
C is the innermost class or interface enclosing the use.
使用实例变量,其声明在文本后出现在即使这些实例变量在范围内,有时也会限制使用。具体来说,如果满足以下所有条件,则为编译时错误:
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:
-
在a中声明实例变量在使用实例变量后,类或接口C以文本方式显示;
The declaration of an instance variable in a class or interface C appears textually after a use of the instance variable;
在C的实例变量初始化程序或实例初始化程序中使用是一个简单名称of C;
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是封闭使用的最里面的类或接口。
C is the innermost class or interface enclosing the use.
在你的第二种情况,使用不是一个简单的名称 - 你明确地得到了这个
。这意味着它不符合上面引用的第二个列表中的第二个项目符号,因此没有错误。
In your second case, the use isn't a simple name - you've got this
explicitly. That means it doesn't comply with the second bullet in the second list quoted above, so there's no error.
如果您将其更改为:
System.out.println("Test string is: " + testString);
...然后它将无法编译。
... then it won't compile.
或者在相反的方向,您可以将静态初始化程序块中的代码更改为:
Or in the opposite direction, you can change the code in the static initializer block to:
System.out.println("Test string is: " + StaticInitialisation.testString);
奇怪,但这就是它的方式。
Odd, but that's the way it goes.
这篇关于为什么两个程序有前向引用错误而第三个没有?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!