问题
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".
Relevant link.
回答1:
After going through some material and discussing with couple of guys offline i found out the following information.
When a java class is getting executed there are few steps which JVM performs few steps sequentially.
- Identify the static members from top to bottom.
- Executes static variables assignments and static blocks from top to bottom.
- Executes the main method.
During these phases there is one such state called RIWO(Read Indirectly Write Only) for a static variable.
During RIWO a variable cannot be accessed directly with its reference. Instead we need to use an indirect way to call certain variables.
for example:
class Riwo
{
static int i = 10;
static
{
System.out.println(i);
}
}
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");
}
}
In the above case the output is
0 block m1 block1 end of code 20 block m1 block 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: illegal forward reference System.out.println(i);
That means we cannot read a static variable directly when it is in RIWO state.We should call the variable indirectly using a method.
回答2:
Static variable have two states
- RIWO (Read Indirectly Write Only)
- RW(Read Write)
.
RIWO: Before assigning original value to static variable, variable state is called as RIWO state.
RW: After assigning original value to static variable, variable state is called as RW state.
Static variable have two type of reading
- Direct Read
- InDirect Read
Direct Read:
Reading static variable from static block directly is called direct reading.
InDirect Reading:
Reading static variable from static block by calling another method is called indirect reading.
Direct Read Example:
Note: when static variable in RIWO state we cant perform Direct Reading.
class DirectReadingExample{
static {
System.out.println("i==="+i);
}
//i is in RIWO state
static int i=100;
//i is in RW State
}
Output: compile time Error:illegal forward reference.
InDirect Read Example:
Note: when static variable in RIWO state we can perform InDirect Reading.
class InDirectReadingExample{
static {
m1();
}
static m1(){
System.out.println("i===="+i);
}
//i is in RIWO state
static int i=100;
//i is in RW State
}
Output: i===0
回答3:
if a variable is just identified by the JVM and original value is not yet assigned then the variable is said to be in RIWO
_______________________that is run without error __________________________________
class Test{
static int num=10;
static{
System.out.print(num);
System.exit(0);
}
}
__________________________that is run within error______________________________
class Test{
static{
System.out.print(num);
}
static int num=10;
}
___________________that is run without error____________________
class Test{
static{
int x=readVar();
}
static int readVar(){
System.out.print(num);
return 0;
}
static int num=10;
}
num is identified by JVM 1st step then default value is given at that time so that value of num variable is 0 because we can't get value directly but we can access indirectly.
来源:https://stackoverflow.com/questions/31779507/what-is-riwo-read-indirectly-write-out-state