Java Error Invalid forward reference

Source: Internet
Author: User

Today in the "Thinking in Java," the fourth chapter refers to the illegal forward reference, so he tried it, the example of the book is a bit to understand, but his own write how also do not understand, so the internet asked a senior, finally understand!

This is the wrong code:

Class bb{    static int a = 0;    Public BB ()    {        a++;        System.out.println ("Execute BB" + a);    public void PrintA ()    {        System.out.println ("a=" + a);}    } public class cc{    static    {        a = new BB ();        A.printa (); Error saying illegal forward reference    }    static bb a = new BB ();    Public staic void Main (String args[])    {        cc c = new CC ();}    }

Why did I initialize a in the static code block and still make an error?

The reason for this involves Java's limitations on member variables during initialization:

The member variable a must be declared before use if it satisfies the following 4 points

      1. Set C as the class or interface that directly contains the member variable
      2. If a appears in C or static member/non-static member initialization or C static or non-static code block
      3. If a is not an lvalue of an assignment inequality
      4. Access by simple name

In my own code, A.printa (), where it appears is a static code block of CC, directly accessible through a simple name (that is, directly using a), and is not an lvalue of an assignment inequality, so an error "illegal forward reference"

This is the source code in the Java language Specification (where Chinese is my own annotation):

Class usebeforedeclaration{static {x = 100;        Ok-assignment, an lvalue int y = x + 1 for an assignment expression;        Error-read before declaration, the right value of an assignment expression is int v = x = 3;        Ok-x at left hand side of assignment, lvalue int z = usebeforedeclaration.x * 2; Ok-not accessed via simple name, is accessed through the class. Static variable, rather than directly accessing object o = new Object () {void F            Oo () {x + +;            }//Ok-occurs in a different class, not in CC's code block or member initialization, but in a new inner class function {x + +;    }//Ok-occurs in a different class, within the code block of an inner class, similar to the previous};        } {j = 200;        Ok-assignment J = j + 1;        Error-right hand side reads before declaration, second right value int k = j = j + 1;        Error-illegal forward reference to J, the third is an rvalue int n = j = 300;      Ok-j at left hand side of assignment, lvalue int h = j + +;    Error-read before declaration, rvalue, and participates in the self-increment operation int L = THIS.J * 3;            Ok-not accessed via simple name is accessed via THIS.J and is not directly accessible with object o = new Object () {void foo ()            {j + +;            }//Ok-occurs in a different class {j = j + 1;    }//Ok-occurs in a different class};    } int w = x = 3;    Ok-x at left hand side of assignment int p = x;        Ok-instance initializers may access static fields static int u = (new Object () {int bar ()        {return x;    }}). bar ();    Ok-occurs in a different class static int x;    int m = j = 4;  Ok-j at left hand side of assignment int o = (new Object () {int bar () {return        J    }}). bar (); Ok-occurs in a different class int J;}

The original address of the question: http://segmentfault.com/q/1010000002569214

This part of the knowledge of the Java documentation: http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.2.3

Thinking in Java note, if there is a wrong place, also look at ^_^

Java Error Invalid forward reference

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.