Why are interfaces static?

前端 未结 4 868
星月不相逢
星月不相逢 2020-12-19 08:33

Why can I not have a interface inside of a inner class? Why are they inherently static? Sorry if it\'s a stupid question, I\'ve tried my best to google this aga

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-19 09:12

    Why can I not have a interface inside of a inner class?

    Because interfaces are implicitly static: JLS §8.5.1:

    A member interface is implicitly static (§9.1.1). It is permitted for the declaration of a member interface to redundantly specify the static modifier.

    and you can't have non-final statics in an inner class.

    Why are they implicitly static?

    Because that's the way they designed it.

    why cannot I declare these in inner classes/local classes?

    Because they're implicitly static.

    the reason we can have static final variables in a interface is because they do not specify the state or any of that sort of the implementation right?

    Right.

    If we lose static and use just a final, we need a instance

    Right.

    which makes no sense cause you can't instantiate a interface.

    Yes you can. You can instantiate a class which implements the interface, or you can instantiate a method-local anonymous implementation of it. The real issue here is multiple inheritance of interfaces.

提交回复
热议问题