Java非静态变量引用错误?

Java非静态变量引用错误?,java,class,static-methods,Java,Class,Static Methods,为什么代码会产生“java:non-static variable a cannot reference from a static context”错误,即使我已经在main方法中创建了该类的实例。我知道静态方法不能使用非静态字段,但是在我创建了类的实例之后,该方法不应该可以使用它吗?您不能从静态上下文引用实例字段 public class Abc { int a = 9; static void print() { System.out.prin

为什么代码会产生“java:non-static variable a cannot reference from a static context”错误,即使我已经在main方法中创建了该类的实例。我知道静态方法不能使用非静态字段,但是在我创建了类的实例之后,该方法不应该可以使用它吗?

您不能从静态上下文引用实例字段

    public class Abc {
     int a = 9;
     static void print() {
         System.out.println(a);
    }
}
class AbcTester {
    public static void main(String[] args) {
        Abc test = new Abc();
        test.a = 8;
        test.print();
    }
}
公共类Abc{

int a=9;//不能从静态上下文引用实例字段

    public class Abc {
     int a = 9;
     static void print() {
         System.out.println(a);
    }
}
class AbcTester {
    public static void main(String[] args) {
        Abc test = new Abc();
        test.a = 8;
        test.print();
    }
}
公共类Abc{

int a=9;//错误在
print
,而不是
main
。想想
a
static
方法中应该是什么。错误在
print
,而不是
main
。想想
a
static
方法中应该是什么。不是我。我确实找到了答案非常感谢你的帮助。谢谢。不是我。我确实觉得答案很有用,感谢你的帮助。谢谢。