java報錯非法的前向引用問題
java報錯非法的前向引用
在看《thinking in java》的時候,第四章提到了非法的前向引用,于是自己試了一下,書中的例子倒是一下就明白了,但是自己寫的一個卻怎么也不明白,于是上網(wǎng)問了一位前輩,終于明白啦!
這個是錯誤的代碼:
class BB { ? ? static int a = 0 ; ? ? public BB() ? ? { ? ? ? ? a++ ; ? ? ? ? System.out.println("執(zhí)行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出現(xiàn)在在C的或靜態(tài)成員/非靜態(tài)成員初始化 或者 C的靜態(tài)或非靜態(tài)代碼塊中
- 如果a不是 一個賦值不等式的左值
- 通過簡單名稱來訪問
在我自己寫的代碼中,a.printA() ;出現(xiàn)的位置是CC的靜態(tài)代碼塊中,通過簡單名稱直接訪問(也就是直接使用a), 并且不是賦值不等式的左值,所以會報錯“非法的前向引用”
這個是java語言規(guī)范中的原文代碼(其中的中文是我自己的標注):
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 , 是通過類.靜態(tài)變量 的形式訪問, 而非直接簡單訪問 ? ? ? ? Object o = new Object() ? ? ? ? { ? ? ? ? ? ? void foo() ? ? ? ? ? ? { ? ? ? ? ? ? ? ? x++; ? ? ? ? ? ? } ? ? ? ? ? ? // ok - occurs in a different class , 不是CC的代碼塊或成員初始化中,而是在一個全新的內(nèi)部類的函數(shù)中 ? ? ? ? ? ? { ? ? ? ? ? ? ? ? x++; ? ? ? ? ? ? } ? ? ? ? ? ? // ok - occurs in a different class , 在一個內(nèi)部類的代碼塊中, 和上一個類似 ? ? ? ? }; ? ? } ? ? { ? ? ? ? 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; }
java中的非法向前引用方式
非法向前引用的例子:
那么為什么類似于i= "1234";這樣的代碼可以呢?
這是因為Java對其中的某些情況做了“特許”,其中有一條就是“通過簡單名稱引用的變量可以出現(xiàn)在左值位置,但不能出現(xiàn)在右值的位置”,所以前面的代碼可以,但System.out.println(i);不行,因為這是一個右值引用。
其目的是避免循環(huán)初始化和其他非正常的初始化行為。
什么是循環(huán)引用,看一下下面這個例子:
privateinti=j; privateintj=i;
如果沒有前面說的強制檢查,那么這兩句代碼就會通過編譯,但是很容易就能看得出來,i和j并沒有被真正賦值,因為兩個變量都是未初始化的(Java規(guī)定所有變量在使用之前必須被初始化)
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java中Pattern.compile函數(shù)的使用詳解
這篇文章主要介紹了Java中Pattern.compile函數(shù)的使用詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08Springboot項目中單元測試時注入bean失敗的解決方案
這篇文章主要介紹了Springboot項目中單元測試時注入bean失敗的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11使用springboot整合websocket實現(xiàn)群聊教程
websocket怎么說呢,就是服務器可以主動向客戶端發(fā)起對話,下面就是springboot整合websocket實現(xiàn)群聊的操作代碼,一起來看一下get新技能吧2021-08-08