"静态方法可能无法与实例字段通信,只能与静态字段通信".我得阅读这个引用的行.当我在这个论坛中研究其他线程时,我发现我们可以在静态方法中使用实例字段,反之亦然.那么,这个引用意味着什么?这是真的吗?
您不能在静态方法中使用非静态(实例)字段.那是因为静态方法与实例无关.
甲static方法是一个每级,而一类可以有许多实例.那么如果你有2个实例,那么静态方法会看到哪一个?
让我们假设这是有效的:
class Foo {
   private int bar;
   public static int getBar() {
      return bar; // does not compile;
   }
}
然后:
Foo foo1 = new Foo();
foo1.bar = 1;
Foo foo2 = new Foo();
foo2.bar = 2;
Foo.getBar(); // what would this return. 1 or 2?