java nested interfaces and inner classes

笑着哭i 提交于 2019-12-05 10:57:10

If an nested class is non-static (i.e. an inner class), this means that each instance of it is bound to an instance of the outer class. As an interface has no instances of its own, it seems to not be useful for the implementing classes to be bound to an outer object, so having it static by default seems reasonable.

I'm not sure why you can't have static non final members in an inner class but since static members aren't bound to any particular object instance it makes no difference whether it is in the inner or outer class.

E.g.

class OuterClass {

  private static int staticMember;

  class InnerClass {

    void incStatic() {
      staticMember++;
    }

  }

}

You can access the static member from the inner class as if it were within the inner class.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!