Why Would a Static Nested Interface Be Used in Java

Why would a static nested interface be used in Java?

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() {...}
});

Why are interfaces static?

Think about what static means - "not related to a particular instance". So, as you point out, a static field of class Foo is a field that does not belong to any Foo instance, but rather belongs to the Foo class itself.

Now think about what an interface is - it's a contract, a list of methods that classes which implement it promise to provide. Another way of thinking about this is that an interface is a set of methods that is "not related to a particular class" - any class can implement it, as long as it provides those methods.

So, if an interface is not related to any particular class, clearly one could not be related to an instance of a class - right?

*Note, as @Owlstead points out, there are ways of defining interfaces within classes. But, for the purposes of wrapping your head around what an interface is (which seems to be what you're working on), I would ignore those possibilities for now as they distract from and possibly obscure the purpose of interfaces in general.

Inline implementation of static nested interfaces

First of all: inner interfaces are "static" by default. It is not possible to have an "inner" interface that wouldn't be static! See here for some more thoughts on that aspect.

From that point of view, interfaces are just that: interfaces. Doesn't matter if they are a top level interface sitting in their own .java file, or if the interface sits within some class or so.

And any interface can be implemented using an anonymous inner class.

That is really all there is to this.

To answer to the comment:

so the inline implementation ie the anonymous class itself becomes an inner class of the interface it implements?

No, an anonymous inner class is an inner class of the class that wraps around it. It happens to implement some interface. It is not an inner class of the interface. Inner is really meant in a "physical" sense, as in lines of code sitting inside other blocks of code.

class Outer {
class/interface Inner {

Inner is called an inner class/interface because it lives inside Outer.

Why do we need inner interfaces, especially with the private access modifier?

Purpose of an inner interfaces in java as below

  1. solving the namespacing issue when the interface has a common name
  2. increasing encapsulation
  3. increasing readability by grouping related interfaces in one place.

A nested interface can be declared as public, private, or protected.
This differs from the top-level interface.The top-level interface must either be declared as public or use the default access level.

Declaration of an inner interface in the body of another interface.

public interface Items {
interface MyList {
}
}

Declaration of an inner interface in the body of another class.

 public class Items{
public interface MyList {
void add(Items items);
String getItesmNames();
}
}

Finally, we can Implement as per below

public class SpecialItems implements Items.MyList  {

}

When a nested interface is used outside of its enclosing scope, it must be qualified by the name of the class or interface of which it is a member.

Advantage of Nested Interface Inside Class When Using Listeners

Being nested makes it very clear that they're closely related to Manager, and used to listen on events fired by the Manager. I would still prefer them as top-level interfaces, in the same package as the Manager class. Making them nested could also be a (bad) cure for many listeners having the same name but used in different contexts.

Making three different interfaces is helpful for two reasons:

  • it doesn't force you to provide dummy implementations of two methods when you're only interested in one kind of event
  • it makes the interface a functional interface, which can thus be implemented as a lambda expression or method reference

Are nested classes inside an interface implicitly static and final?

Lets find out. Lets create structure like:

interface Interface{
class Foo{}
}

Now we can test:

System.out.println("static: " + Modifier.isStatic(Interface.Foo.class.getModifiers()));
System.out.println("final: " + Modifier.isFinal(Interface.Foo.class.getModifiers()));

which prints:

static: true
final: false

So nested classes are implicitly static, but not final.

We can confirm it also by adding class Bar extends Foo{} to our interface. final classes can't be extended but since such code compiles fine it means Foo is not final.



Related Topics



Leave a reply



Submit