今天在看《thinking in java》的时候,第四章提到了非法的前向引用,于是自己试了一下,书中的例子倒是一下就明白了,但是自己写的一个却怎么也不明白,于是上网问了一位前辈,终于明白啦!
这个是错误的代码:
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点,就必须在使用前必须对该成员变量进行声明
在我自己写的代码中,a.printA() ;出现的位置是CC的静态代码块中,通过简单名称直接访问(也就是直接使用a), 并且不是赋值不等式的左值,所以会报错“非法的前向引用”
这个是java语言规范中的原文代码(其中的中文是我自己的标注):
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笔记,如果有不对的地方,还望指正^_^
原文:http://www.cnblogs.com/iamzhoug37/p/4306304.html