为什么当我this在变量声明中使用引用时,没有出现非法正向引用?有this和没有声明之间有什么区别?
由于非法前向引用,以下示例无法编译:
class FailsToCompile {
int a = b; //illegal forward reference
int b = 10;
}
Run Code Online (Sandbox Code Playgroud)
通过排位赛使用b的this编译错误消失。
class Compiles {
int a = this.b; //that's ok
int b = 10;
}
Run Code Online (Sandbox Code Playgroud) java ×1