Java 类格式错误 : Method "<error>" class has illegal signature

标签 java static

我在第 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/

相关文章:

java - 如何响应来自eventbus消费者的http请求

java - Ant 在 javac 处失败且没有消息

c++ - 使用不带对象的类函数

c - 为什么使用存储类说明符来确定两个独立的属性?

C++ 使用静态关键字或索引模板处理许多派生类

java - 我想在特定列中排列整个单元格,而不是单个单元格

java - 为什么我在 python 中缺少一个在我从 java 进行系统调用时应该安装的模块?

java - JDesktopPane 和 BorderLayout

java - Java 静态方法没有 RMI

c# - INotifyPropertyChanged 和静态属性