Illegal Forward Reference Error In Java

Illegal forward reference error in java is a compile time error. You will encounter with this error when you try to use a field before it is defined just like in the below example,

1
2
3
4
5
6
7
8
9
10
class A
{
    static
    {
        System.out.println(i);  //You will get compile time error here
    //  can not refer a field before it is defined
    }
    static int i;
}

That means we can’t use a field before it is defined. But there is an interesting point, We can initialize a field before it is defined. Have look at below code. There will be no compile time errors here.

1
2
3
4
5
6
7
8
9
class A
{
    static
    {
        i = 10;   //A field can be initialized before it is defined.
    }
    static int i;
}

Above two examples can be summarized like this, We can’t use a field before it is defined but we can initialize a field before it is defined.

Let’s have a look at some other examples.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class A
{
    static int i;
    static
    {
           System.out.println(i);
    //     System.out.println(j);  can't use a field before it is defined
    //     i=j;                   even you can't use it to initialize other fields
           j=i;                    //but, can be initialized.
    }
    static int j;
}

Even you can’t use undefined field to initialize other fields (Line 9 in the above example).

Important Note :  In the assignment statement, you can use undefined field on LHS only but not on RHS.

Illegal forward reference error applies to non-static variables also.

1
2
3
4
5
6
7
8
9
public class A
{
    {
    //    System.out.println(i); can't use a field before it is defined
        i = 20;               //but, can be initialized
    }
    int i;
}

In the case of local variables, neither you can use it nor you can initialize it before it is defined.

1
2
3
4
5
6
7
8
9
class A
{
    void methodOfClassA()
    {
    //    System.out.println(i); can't use a local field before it is defined
    //    i = 10;                can't initialize a local field before it is defined
        int i;
    }
}

Illegal forward reference issue can be analysed like this. Consider the following statements.

1
2
3
int a = 10;
int b = c;
int c = 30;

Compiler always compiles the program from top to bottom. In the first statement, compiler declares variable ‘a’ and assigns value 10 to it. In the second statement, it declares variable ‘b’ and tries to find where is ‘c’. But, it is not declared yet. Therefore, it gives illegal forward reference error. Illegal forward reference is nothing but you are referring to something in advance that does not exist yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s