Why would a static inner interface be used in Java?

Q:
I have just found a static inner interface in our code-base.

  1. class Foo {
  2.     public static interface Bar {
  3.         /* snip */
  4.     }
  5.     /* snip */
  6. }
复制代码

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:

  1. public class Baz implements Foo.Bar {
  2.    ...
  3. }
复制代码

In most ways, this isn't different from a static inner class.

你可能感兴趣的:(java)