java 非法转发引用java问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14624919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Illegal forward Reference java issue
提问by Timofei
Could anyone explain what is wrong with this code:
谁能解释一下这段代码有什么问题:
public class Base {
static {
i = 1;
System.out.println("[Base]after static init block i=" + i);// LINE 1
System.out.println("*************************************");
System.out.println();
}
static int i;
public static void main(String[] args) {
System.out.println(Base.i);
}
}
If I comment LINE 1 - everything is OK and Base.main method prints "1". If LINE 1 - is not commented, got compile time error: "illegal forward reference". So, as i understand in static init block I can set value for i, but not read. Could anyone explain why?
如果我评论 LINE 1 - 一切正常,Base.main 方法打印“1”。如果第 1 行 - 未注释,则会出现编译时错误:“非法前向引用”。所以,正如我在静态初始化块中所理解的,我可以为 i 设置值,但不能读取。谁能解释一下为什么?
回答by assylias
This is because of the restrictions on the use of Fields during Initialization. In particular, the use of static fields inside a static initialization block before the line on which they are declared can only be on the left hand side of an expression (i.e. an assignment), unless they are fully qualified (in your case Base.i
).
这是因为在 Initialization 期间对 Fields 的使用有限制。特别是,在声明它们的行之前的静态初始化块内使用静态字段只能在表达式的左侧(即赋值),除非它们是完全限定的(在您的情况下Base.i
)。
So for example: if you insert int j = i;
right after i = 1;
you would get the same error.
例如:如果您在插入int j = i;
后立即插入,i = 1;
则会出现相同的错误。
The obvious way to solve the issue is to declare static int i;
beforethe static initialization block.
解决问题的明显方法是在静态初始化块static int i;
之前声明。
回答by Massimiliano Peluso
"Illegal forward reference" means that you are trying to use a variable before it is defined.
“非法前向引用”意味着您试图在定义变量之前使用它。
The behavior you observe is a symptom of a javac bug(see this bug report). The problem appears to be fixed in newer versions of the compiler, e.g. OpenJDK 7.
您观察到的行为是 javac 错误的症状(请参阅此错误报告)。该问题似乎已在较新版本的编译器中得到解决,例如 OpenJDK 7。
have a look at
看一下
回答by kmb
You can add Base to your i variable in static block or you have to declare static int i before the block. Other solution is to create static method instead of static block.
您可以将 Base 添加到静态块中的 i 变量,或者您必须在块之前声明 static int i 。其他解决方案是创建静态方法而不是静态块。
static {
Base.i = 1;
System.out.println("[Base]after static init block i=" + Base.i);// LINE 1
System.out.println("*************************************");
System.out.println();
}
回答by TheWhiteRabbit
Change your code to:
将您的代码更改为:
int i;
static {
i = 1;
System.out.println("[Base]after static init block i=" + i);// LINE 1
System.out.println("*************************************");
System.out.println();
}
回答by Neeraj
A variable should always be textually declared before used else Illegal forward Reference comes into picture. Only Exception to this Statement is : If prior to declaration it is used on LHS of expression. Ex :
Snippet 1:
static{
x = 10;
}
static int x;
Above snippet will work.
Snippet 2:
static{
System.out.println("Value of x: " + x)
}
static int x;
This will give CTE, because x isnt LHS of expression.
Keeping those conditions in mind we can avoid Illegal Forward ref issue in our code.
Thanks