我在第 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
}
}
最佳答案
您无法在方法内声明静态字段:
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
}
}
关于Java 类格式错误 : Method "<error>" class has illegal signature,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16375126/