java 报错非法的前向引用
今天在看《thinking in java》的时候,第四章提到了非法的前向引用,于是自己试了一下,书中的例子倒是一下就明白了,但是自己写的一个却怎么也不明白,于是上网问了一位前辈,终于明白啦!
这个是错误的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | class BB { static int a = 0 ; public BB() { a++ ; System.out.println( "执行BB" + a) ; } public void printA() { System.out.println( "a= " + a) ; } } public class CC { static { a = new BB() ; a.printA() ; //报错说非法的前向引用 } static BB a = new BB() ; public staic void main(String args[]) { CC c = new CC() ; } } |
为什么我在 static 代码块中对 a 进行了初始化,仍然报错呢?
原因就涉及了 java 对于初始化过程中对成员变量的限制:
成员变量 a 如果满足如下的 4 点,就必须在使用前必须对该成员变量进行声明
- 设定 C 为直接包含该成员变量的类或者接口
- 如果 a 出现在在 C 的或静态成员 / 非静态成员初始化 或者 C 的静态或非静态代码块中
- 如果 a 不是 一个赋值不等式的左值
- 通过简单名称来访问
在我自己写的代码中,a.printA(); 出现的位置是 CC 的静态代码块中,通过简单名称直接访问 ( 也就是直接使用 a), 并且不是赋值不等式的左值,所以会报错“非法的前向引用”
这个是 java 语言规范中的原文代码 (其中的中文是我自己的标注):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | class UseBeforeDeclaration { static { x = 100 ; // ok - assignment , 赋值表达式的左值 int y = x + 1 ; // error - read before declaration , 赋值表达式的右值 int v = x = 3 ; // ok - x at left hand side of assignment , 左值 int z = UseBeforeDeclaration.x * 2 ; // ok - not accessed via simple name , 是通过类.静态变量 的形式访问, 而非直接简单访问 Object o = new Object() { void foo() { x++; } // ok - occurs in a different class , 不是CC的代码块或成员初始化中,而是在一个全新的内部类的函数中 { x++; } // ok - occurs in a different class , 在一个内部类的代码块中, 和上一个类似 }; } { j = 200 ; // ok - assignment j = j + 1 ; // error - right hand side reads before declaration , 第二个右值 int k = j = j + 1 ; // error - illegal forward reference to j , 第三个是右值 int n = j = 300 ; // ok - j at left hand side of assignment , 左值 int h = j++; // error - read before declaration , 右值, 并参与了自增运算 int l = this .j * 3 ; // ok - not accessed via simple name 通过this.j进行访问, 非直接简单访问 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; } |
// 提问的原文地址: http://segmentfault.com/q/1010000002569214
// 这部分知识的 java 文档: http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.2.3
//thinking in java 笔记,如果有不对的地方,还望指正 ^_^