Why illegal forward reference error not shown while using static variable with class name

In the below code while accessing a static variable with class name it doesn't throw a forward reference error but accessing it without class name does.

Why this doesn't happen when accessing with class name?

class Test{
    static {
        System.out.println(a); // shows error
        a = 99; // and this line too doesn't give error  
        System.out.println(Test.a); // this line doesn't
    }
    static int a = 10;  
    static{
        System.out.println(a);
    }
}

The rules for forward reference is defined in JLS §8.3.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:

  • The declaration of a class variable in a class or interface C appears textually after a use of the class variable;

  • 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 is the innermost class or interface enclosing the use.

So, basically your first Sysout(), satisfies all the above 4 conditions, and hence it's a compile time error.

In the 2nd Sysout(), you're accessing a using it's qualified name, rather than simple name, which as per the above rules is allowed.

Now, the reason for this would be, when you access Test.a, the compiler is sure that Test class has been loaded and all static fields have been initialized, so it can access the field a. But while accessing a on simple name, the compiler isn't sure that initializer for a has already run or not, since it might still be in process of loading the class.

Consider the following process of loading of class:

  • When a class is loaded, memory is allocated for all the static variables declared in it. At this point, the variable a has got its memory allocated (declaration done)
  • Then all the static initializers run in the order of occurrence.
    • First statement is Sysout(a);. a hasn't been initialized yet, so you can't access it. (error)
    • Second statement is a = 99. Here you're actually initializing the variable a. Perfectly fine.
    • Third one is Sysout(Test.a) - reasoning for this is already posted above. Compiler knows Test is already loaded.
    • Then static int a = 10 is executed. It re-initializes a to 10. Remember, declaration part is already taken care of in first step.

First , let's see what JLS have to say for illegal forward references.

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:

The declaration of a class variable in a class or interface C appears textually after a use of the class variable;

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 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:

The declaration of an instance variable in a class or interface C appears textually after a use of the instance variable;

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 is the innermost class or interface enclosing the use.

It defines what is a illegal forward reference for each - static and instance variables. However, definition seems to be same for both of them. Since your question asks about static, we will dig deep into this only.

1) See, for a static variable, there is declaration and there is initialization.

 static int a; //only declaration
 static int b = 10; //both declaration and initialization

2) Use of class variables whose declarations appear textually after the use is sometimes restricted, even though these class variables are in scope.

What does it explains to us ? It says that there are cases when we can use static variables even if we declare them afterwards. And in some cases this wont be allowed. So, what are the cases that this won't be allowed ?

If the following 4 conditions are true at the same time. Else you are free to use it even though you have declared it afterwards.

a) The declaration of a class variable in a class or interface C appears textually after a use of the class variable;

b) The use is a simple name in either a class variable initializer of C or a static initializer of C;

c) The use is not on the left hand side of an assignment;

d) C is the innermost class or interface enclosing the use.

Well, the a) point is simple. It says that you must be looking at these rules only when you want to use a static variable before declaring it else why would you dig up into this JLS.

The b) point is that if you use simple name like boy [and not class name appended to it like MyClass.boy only then you might enter the problem of illegal forward reference else you are safe my friend. But just this condition don't qualifies for Illegal forward reference else your a=99 in you code would have given us error immediately. There are 2 more conditions that HAVE to be true for this error to generate . If the below 2 does not qualify, you are allowed to use it like this.]

The point c) is pretty straightforward. Are you not using in left hand side of assignment ? If we look at a=99 , Nope !! System.out.println(a) - This is not even an assignment. Hence no left hand assignment is true.

The point d) is also simple. It just tell you which class/Interface C he means in definition.Your C = Test.

Now let's revisit your code. At each line of code, I will comment True+False+True+false something like this meaning that for each line what point a), b) , c) ,d) will give to each of line. Okay ?

class Test {

    static {
        System.out.println(a); // True + True + True +True ; Illegal forward reference
        a = 99; // True +  True + False +True ; No illegal forward reference
        System.out.println(Test.a); // True + False + True + True No illegal forward reference
    }

    static int a = 10;

    static {
        System.out.println(a);
    }
}

The next question one may think is now what it will print ? What values will it take if we use before declaring it ?

Now we come to rules of static initialization. Here, we will go according to your code.

When class is loaded and no illegal forward reference is not there , all the static variables have been initialized to default value and are present in memory.

Now, after this static initialization takes place.

class Test {

    static {
        System.out.println(Test.a);  // prints 0
        a = 99; // a get 99
        System.out.println(Test.a); // prints 99
    }

    static int a = 10;

    static {
        System.out.println(a); // prints 10
    }

    public static void main(String[] args) {

    }
}