看到网上一道面试题挺有意思的
**
源码:
**
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);
}
}
这个题目考了一个非常小的知识点:在java类中的“方法体”中声明变量时,只能用final修饰该变量(或者干脆不写)。这里用到了static去修饰i,所以会出异常。