为什么这个JAVA不能运行

来源:百度知道 编辑:UC知道 时间:2024/07/07 16:33:40
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);
}
}

1 去掉static
2 public class Test {
static int i = 0; //声明放在这里
public int aMethod() {

i++;
return i;
}
public static void main (String args[]) {
Test test = new Test();
test.aMethod();
int j = test.aMethod();
System.out.println(j);
}
}

static int i = 0; static删除就好了

static变量在方法内部才能调用
你在main方法里面调用了当然不行!

把 static去掉就可以运行了!