Illegal forward Reference java issue

"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.

have a look at

Illegal forward reference error for static final fields


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).

So for example: if you insert int j = i; right after i = 1; you would get the same error.

The obvious way to solve the issue is to declare static int i; before the static initialization block.

Tags:

Java