15

Could anyone explain what is wrong with this code:

    public class Base {


    static {
        i = 1;
        System.out.println("[Base]after static init block i=" + i);// LINE 1
        System.out.println("*************************************");
        System.out.println();
    }
    static int i;



    public static void main(String[] args) {
        System.out.println(Base.i);
    }
}

If I comment LINE 1 - everything is OK and Base.main method prints "1". If LINE 1 - is not commented, got compile time error: "illegal forward reference". So, as i understand in static init block I can set value for i, but not read. Could anyone explain why?

Timofei
  • 420
  • 1
  • 5
  • 14

6 Answers6

27

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.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • I tried this same example but without static keyword and it behaved the same way as this code. So does it also follow same rule? What is the use of allowing only on LHS rather we can throw compile time error. – prashanth Jul 04 '20 at 18:19
  • Yes, the rule is the same for static and instance variables. Follow the link in my answer. – assylias Jul 06 '20 at 06:14
  • Side note: Mocks have the exact same limitation. – Egor Hans Oct 04 '21 at 06:49
11

"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

Community
  • 1
  • 1
Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
3

You can add Base to your i variable in static block or you have to declare static int i before the block. Other solution is to create static method instead of static block.

static {
    Base.i = 1;
    System.out.println("[Base]after static init block i=" + Base.i);// LINE 1
    System.out.println("*************************************");
    System.out.println();
}
kmb
  • 871
  • 5
  • 17
  • 34
2

Change your code to:

int i;
static {
    i = 1;
    System.out.println("[Base]after static init block i=" + i);// LINE 1
    System.out.println("*************************************");
    System.out.println();
}
TheWhiteRabbit
  • 15,480
  • 4
  • 33
  • 57
0
A variable should always be textually declared before used else Illegal forward Reference comes into picture. Only Exception to this Statement is : If prior to declaration it is used on LHS of expression. Ex :
Snippet 1: 
 static{
   x = 10;
}
static int x;  
Above snippet will work.

Snippet 2:
static{
  System.out.println("Value of x: " + x)  
}
static int x;
  This will give CTE, because x isnt LHS of expression.

Keeping those conditions in mind we can avoid Illegal Forward ref issue in our code.


Thanks
Neeraj
  • 171
  • 2
  • 11
0

public class Base {

static {
    i = 1;
    System.out.println("[Base]after static init block i=" +Base.i);// illegal start of expression
    System.out.println("*************************************");
    System.out.println();
}
static int i;



public static void main(String[] args) {
    System.out.println(Base.i);
}

}