java基础---非法前向引用
来源:互联网 发布:蜂窝移动数据里没有app 编辑:程序博客网 时间:2024/06/08 03:37
public class Test {
static { i = 100; // System.out.println("================================"+i); } public static int i=10; public static void main(String[] args) { System.out.println("此时 i 的输出为" + i); } }
上面的注释这行代码为什么会报错:非法向前引用?
这个时候我debug都可以看到i的值是100.为什么不能打印出来呢?
原因就涉及了java对于初始化过程中对成员变量的限制:
成员变量a如果满足如下的4点,就必须在使用前必须对该成员变量进行声明
设定Test为直接包含该成员变量的类或者接口
如果i出现在在Test的或静态成员/非静态成员初始化 或者 Test的静态或非静态代码块中
如果i不是 一个赋值不等式的左值
通过简单名称来访问
在我自己写的代码中,a.printA() ;出现的位置是Test的静态代码块中,通过简单名称直接访问(也就是直接使用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;}
阅读全文
0 0
- java基础---非法前向引用
- Javac编译时报“非法的前向引用”,以及Eclipse自带的Java编译器的缺陷
- 前向引用声明
- 前向引用声明
- 前向引用声明
- 【C++】前向引用申明
- C++前向引用实例
- C++前向引用声明
- c++ 前向引用详细解说
- 关于前向引用的问题
- 转:使用类别创建前向引用
- 何时要使用前向引用@class
- iOS开发类别的前向引用
- 私有方法的前向引用
- static 非法向前引用
- Java基础-----Java引用
- 【Java基础向】多态
- 到底是前向引用,还是包含头文件
- Python基础入门之函数一
- CentOS 6.x启动时激活网卡
- maven 继承关系和聚合
- arXiv领域分类
- 一个Myeclipse同时配置多个Tomcat
- java基础---非法前向引用
- C语言为什么~0 == -1?
- el表达式中 前台无法获取后台传输的值
- “大超人”——基于医疗大数据的的人工智能医生CDSS
- 杂例子
- 04.奇特的一生(笔记)
- linux常用指令
- 初学Html5+CSS之表单的省级联动
- NestedScrollView