问题描述
我正在阅读关于静态流量控制的内容并且遇到了RIWO概念。有人可以用简单的术语和代码样本解释这个吗?
I was reading about the static flow control and came across the RIWO concept. Can someone explain this with simple terminology and perhaps a code sample?
这与错误非法转发参考有关。
This is related to the error "Illegal forward reference".
。
推荐答案
在浏览了一些材料并与几个人离线讨论后,我发现了以下信息。
After going through some material and discussing with couple of guys offline i found out the following information.
当java类被执行时,JVM按顺序执行几个步骤的步骤很少。
When a java class is getting executed there are few steps which JVM performs few steps sequentially.
- 从上到下识别静态成员。
- 从上到下执行静态变量赋值和静态块。
- 执行main方法。
在这些阶段中,有一个名为RIWO(只读间接写入)的静态变量状态。
During these phases there is one such state called RIWO(Read Indirectly Write Only) for a static variable.
期间RIWO变量无法通过其引用直接访问。相反,我们需要使用间接方式来调用某些变量。
During RIWO a variable cannot be accessed directly with its reference. Instead we need to use an indirect way to call certain variables.
例如:
class Riwo
{
static int i = 10;
static
{
System.out.println(i);
}
}
在上述情况下,输出为10.
In the above case the output is 10.
class Riwo {
static int i = 10;
static {
m1();
System.out.println("block1");
}
public static void main(String... args) {
m1();
System.out.println("block main");
}
public static void m1() {
System.out.println(j);
System.out.println("block m1");
}
static int j = 20;
static {
System.out.println("end of code");
}
}
在上述情况下,输出为
0
block m1
block1
代码结束
20
block m1
block main
0block m1block1end of code20block m1block main
class Riwo
{
static
{
System.out.println(i);
System.out.println("block1");
}
static int i = 10;
public static void main(String... args)
{
System.out.println("main block");
}
}
在上面的例子中,我们得到以下编译时错误
In the above case we get the following compile time error
Riwo.java:5:非法转发参考
System.out.println(i);
Riwo.java:5: illegal forward reference System.out.println(i);
这意味着当它处于RIWO状态时我们无法直接读取静态变量。我们应该使用方法间接调用变量。
That means we cannot read a static variable directly when it is in RIWO state.We should call the variable indirectly using a method.
这篇关于什么是RIWO(读取间接写出)状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!