题目描述
以下程序运行结果为
题目来源及自己的思路
某面试题
相关代码
// 请把代码文本粘贴到下方(请勿用图片代替代码)
public class Test {
public int aMethod() {
static int i = 0;
i++;
return i;
}
public static void main (String args[]) {
Test test = new Test();
test.aMethod();
int j = test.aMethod();
System.out.println(j);
}
}
你期待的结果是什么?实际看到的错误信息又是什么?
A. 0
B. 1
C. 2
D. 编译失败
请给出结果并说明原因
D:编译失败
static不能修饰方法内的变量,方法体类的变量只能在方法内可见。
而static修饰的成员变量或者方法表示属于整个类
所以相互矛盾,static不能修饰方法内的变量