今天在看《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點,就必須在使用前必須對該成員變量進行聲明
-
- 設定C為直接包含該成員變量的類或者接口
- 如果a出現在在C的或靜態成員/非靜態成員初始化 或者 C的靜態或非靜態代碼塊中
- 如果a不是 一個賦值不等式的左值
- 通過簡單名稱來訪問
在我自己寫的代碼中,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筆記,如果有不對的地方,還望指正^_^
