views:

7129

answers:

9

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?

+9  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.

Clinton R. Nixon
+1  A: 

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

Map.Entry

also this may be useful

Static Nested Inerfaces blog Entry

PintSizedCat
+2  A: 

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)

basszero
A: 

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.

Vordreller
I know what static means. I just have never ever seen it on an interface before. Therefore your question is off-topic - sorry
Mo
+1  A: 

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.

Skizz

Skizz
+16  A: 

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() {...}
});
Jesse Glick
+3  A: 

Inner interfaces are implicitly static. The static modifier in your example can be removed without changing the semantics of the code. See also the relevant part of the Java Language Specification

Bas Leijdekkers
+7  A: 

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).

ColinD
Thank you - that is a very reasonable use for those...
Mo
+1  A: 

In Jesse Glick's answer, what this mean: (From source code - bytecode or reflection can access Foo.Bar even if Foo is package-private!).

Kaillash
I know this should be a comment just below "Jesse Glick" answer but I am new to stackoverflow and I failed to find how to comment, thats why this is coming as answer. Sorry for this !!
Kaillash
@Kaillash - don't stress it. The ability to add comments to *other people's* posts comes at around 50 rep. Which won't take long.
Marc Gravell
Kaillash, private methods can be accessed through relfection (in the reflect package) and through directly accessing the bytecode of the generated .class files.
gmoore