public class Test2{

来源:百度知道 编辑:UC知道 时间:2024/06/20 18:56:25
public class Test2{
public static int aMethod() {
static int i = 0;

return i;
}
public static void main(String[] args) {
static Test2 test = new Test2();
test.aMethod();
int j = test.aMethod();
System.out.println(j);
}
}
有什么错

静态方法中只能使用静态变量
静态方法中声明的变量就是静态变量,无需加static关键字

变量前面的 static 去掉

public class Test2 {
public static int aMethod() {
int i = 0;

return i;
}

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