我在第1行和第2行遇到错误。第1行说非法表达开头。 我不明白为什么第1行是非法的
public class MyArt {
public static void main(String argv[]) {
MyArt m = new MyArt();
m.amethod();
}
public void amethod() {
static int i; // line 1
System.out.println (i); // line 2
}
}
答案 0 :(得分:2)
你不能在方法中声明静态字段:
public class MyArt {
public static void main(String argv[]) {
MyArt m = new MyArt();
m.amethod();
}
//you can very well have non-static method since you are
//calling it through MyArt object m
public void amethod() {
int i=0; // REMOVED STATIC, otherwise program won't compile
System.out.println (i); // line 2, if not initialized compilation will fail where the variable is refrenced
}
}