1

我在第 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
    }
}
4

1 回答 1

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

        }
    }
于 2013-05-04T14:09:52.520 回答