如下代码的 输出结果是什么?
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 编译失败
考点:考察求职者对局部变量的理解
出现频率:★★★★★
【面试题分析】
错误在static int i = 0;这句,static不能用于局部变量,局部变量是有相对短的生命周期,而static变量的生命周期和整个程序一样,不方便内存回收。Java中不允许定义静态局部变量。
所以参考答案是(D)