Why would a static nested interface be used in Java?

JavaInterfaceStatic

Java Problem Overview


I have just found a static nested 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?

Java Solutions


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

Solution 2 - Java

The question has been answered, but one good reason to use a nested 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 a nested interface and have those other classes implement Foo.Listener (a nested class Foo.Event isn't bad along with this).

Solution 3 - Java

Member interfaces are implicitly static. The static modifier in your example can be removed without changing the semantics of the code. See also the the Java Language Specification 8.5.1. Static Member Type Declarations

Solution 4 - Java

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.

Solution 5 - Java

Jesse's answer is close, but I think that there is a better code to demonstrate why an inner interface may be useful. Look at the code below before you read on. Can you find why the inner interface is useful? The answer is that class DoSomethingAlready can be instantiated with any class that implements A and C; not just the concrete class Zoo. Of course, this can be achieved even if AC is not inner, but imagine concatenating longer names (not just A and C), and doing this for other combinations (say, A and B, C and B, etc.) and you easily see how things go out of control. Not to mention that people reviewing your source tree will be overwhelmed by interfaces that are meaningful only in one class.So to summarize, an inner interface enables the construction of custom types and improves their encapsulation.

class ConcreteA implements A {
 :
}

class ConcreteB implements B {
 :
}

class ConcreteC implements C {
 :
}

class Zoo implements A, C {
 :
}

class DoSomethingAlready {
  interface AC extends A, C { }

  private final AC ac;

  DoSomethingAlready(AC ac) {
    this.ac = ac;
  }
}

Solution 6 - Java

To answer your question very directly, look at Map.Entry.

http://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html">Map.Entry</a>

also this may be useful

http://littletutorials.com/2008/03/06/static-nested-interfaces/">Static Nested Inerfaces blog Entry

Solution 7 - Java

Typically I see static inner classes. Static inner classes cannot reference the containing classes wherease non-static classes can. Unless you're running into some package collisions (there already is an interface called Bar in the same package as Foo) I think I'd make it it's own file. It could also be a design decision to enforce the logical connection between Foo and Bar. Perhaps the author intended Bar to only be used with Foo (though a static inner interface won't enforce this, just a logical connection)

Solution 8 - Java

If you will change class Foo into interface Foo the "public" keyword in the above example will be also redundant as well because

> interface defined inside another interface will implicitly public > static.

Solution 9 - Java

In 1998, Philip Wadler suggested a difference between static interfaces and non-static interfaces.

> So far as I can see, the only difference in making an interface non-static is that it can now include non-static inner classes; so the change would not render invalid any existing Java programs.

For example, he proposed a solution to the Expression Problem, which is the mismatch between expression as "how much can your language express" on the one hand and expression as "the terms you are trying to represent in your language" on the other hand.

An example of the difference between static and non-static nested interfaces can be seen in his sample code:

// This code does NOT compile
class LangF<This extends LangF<This>> {
	interface Visitor<R> {
	    public R forNum(int n);
    }

	interface Exp {
        // since Exp is non-static, it can refer to the type bound to This
	    public <R> R visit(This.Visitor<R> v);
    }
}

His suggestion never made it in Java 1.5.0. Hence, all other answers are correct: there is no difference to static and non-static nested interfaces.

Solution 10 - Java

In Java, the static interface/class allows the interface/class to be used like a top-level class, that is, it can be declared by other classes. So, you can do:

class Bob
{
  void FuncA ()
  {
    Foo.Bar foobar;
  }
}

Without the static, the above would fail to compile. The advantage to this is that you don't need a new source file just to declare the interface. It also visually associates the interface Bar to the class Foo since you have to write Foo.Bar and implies that the Foo class does something with instances of Foo.Bar.

A description of class types in Java.

Solution 11 - Java

Static means that any class part of the package(project) can acces it without using a pointer. This can be usefull or hindering depending on the situation.

The perfect example of the usefullnes of "static" methods is the Math class. All methods in Math are static. This means you don't have to go out of your way, make a new instance, declare variables and store them in even more variables, you can just enter your data and get a result.

Static isn't always that usefull. If you're doing case-comparison for instance, you might want to store data in several different ways. You can't create three static methods with identical signatures. You need 3 different instances, non-static, and then you can and compare, caus if it's static, the data won't change along with the input.

Static methods are good for one-time returns and quick calculations or easy obtained data.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMo.View Question on Stackoverflow
Solution 1 - JavaJesse GlickView Answer on Stackoverflow
Solution 2 - JavaColinDView Answer on Stackoverflow
Solution 3 - JavaBas LeijdekkersView Answer on Stackoverflow
Solution 4 - JavaClinton DreisbachView Answer on Stackoverflow
Solution 5 - Javauser1982892View Answer on Stackoverflow
Solution 6 - JavaHenry BView Answer on Stackoverflow
Solution 7 - JavabasszeroView Answer on Stackoverflow
Solution 8 - JavaDanylo VolokhView Answer on Stackoverflow
Solution 9 - JavaPindatjuhView Answer on Stackoverflow
Solution 10 - JavaSkizzView Answer on Stackoverflow
Solution 11 - JavaVordrellerView Answer on Stackoverflow