The static keyword in the above example is redundant (a nested interface is automatically “static”) and can be removed with no effect on semantics; I would recommend it be removed. The same goes for “public” on interface methods and “public final” on interface fields – the modifiers are redundant and just add clutter to the source code.

Either way, the developer is simply declaring an interface named Foo.Bar. There is no further association with the enclosing class, except that code which cannot access Foo will not be able to access Foo.Bar either. (From source code – bytecode or reflection can access Foo.Bar even if Foo is package-private!)

It is acceptable style to create a nested interface this way if you expect it to be used only from the outer class, so that you do not create a new top-level name. For example:

public class Foo {
    public interface Bar {
        void callback();
    }
    public static void registerCallback(Bar bar) {...}
}
// ...elsewhere...
Foo.registerCallback(new Foo.Bar() {
    public void callback() {...}
});

Inner interfaces are implicitly static. The static modifier in your example can be removed without changing the semantics of the code. 

The question has been answered, but one good reason to use an inner interface is if its function is directly related to the class it is in. A good example of this is a Listener. If you had a class Foo and you wanted other classes to be able to listen for events on it, you could declare an interface named FooListener, which is ok, but it would probably be more clear to declare an inner interface and have those other classes implement Foo.Listener (an inner class Foo.Event isn't bad along with this).
interface inside of outer class

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据