If a static variable is in RIWO (Read Indirectly Write Only) state. the static variable can not be accessed directly.
here is the code
class Test {
static{
System.out.println(x);
}
static int x = 10;
public static void main(String[] args) {
}
}
in this case illegal forward reference compile time error is coming.
but if you are using the class name to access the static variable, it can be accessed.
here is the code example
class Test {
static{
System.out.println(Test.x);
}
static int x = 10;
public static void main(String[] args) {
}
}
answer is : 0
how is this possible ? isn't this a direct access ?