Q:
I have just found a static inner interface in our code-base.
- class Foo {
- public static interface Bar {
- /* snip */
- }
- /* snip */
- }
I have never seen this before. The original developer is out of reach. Therefore I have to ask SO:
What are the semantics behind a static interface? What would change, if I remove the static? Why would anyone do this?
A:
An inner interface has to be static in order to be accessed. The interface isn't associated with instances of the class, but with the class itself, so it would be accessed with Foo.Bar, like so:
- public class Baz implements Foo.Bar {
- ...
- }
In most ways, this isn't different from a static inner class.
本文解释了静态内部接口的语义,即它与类本身而非其实例相关联,并通过示例展示了如何使用静态内部接口。此外,还探讨了将其声明为静态的原因及移除static关键字的影响。
9412

被折叠的 条评论
为什么被折叠?



