<p>I have just found a static nested interface in our code-base.</p>

<pre><code>class Foo {
    public static interface Bar {
        /* snip */
    }
    /* snip */
}
</code></pre>

<p>I have never seen this before. The original developer is out of reach. Therefore I have to ask SO:</p>

<p>What are the semantics behind a static interface? What would change, if I remove the <code>static</code>? Why would anyone do this?</p>

[{'answer_id': 71654, 'author': 'Clinton Dreisbach', 'author_id': 6262, 'author_profile': 'https://Stackoverflow.com/users/6262', 'pm_score': 3, 'selected': False, 'text': "<p>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 <code>Foo.Bar</code>, like so:</p>\n\n<pre><code>public class Baz implements Foo.Bar {\n   ...\n}\n</code></pre>\n\n<p>In most ways, this isn't different from a static inner class.</p>\n"}, {'answer_id': 71662, 'author': 'Henry B', 'author_id': 6414, 'author_profile': 'https://Stackoverflow.com/users/6414', 'pm_score': 2, 'selected': False, 'text': '<p>To answer your question very directly, look at Map.Entry.</p>\n\n<p><a href="http://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html" rel="nofollow noreferrer">Map.Entry</a></p>\n\n<p>also this may be useful</p>\n\n<p><a href="http://littletutorials.com/2008/03/06/static-nested-interfaces/" rel="nofollow noreferrer">Static Nested Inerfaces blog Entry</a></p>\n'}, {'answer_id': 71670, 'author': 'basszero', 'author_id': 287, 'author_profile': 'https://Stackoverflow.com/users/287', 'pm_score': 0, 'selected': False, 'text': "<p>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)</p>\n"}, {'answer_id': 71699, 'author': 'Vordreller', 'author_id': 11795, 'author_profile': 'https://Stackoverflow.com/users/11795', 'pm_score': -1, 'selected': False, 'text': '<p>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.</p>\n\n<p>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.</p>\n\n<p>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.</p>\n\n<p>Static methods are good for one-time returns and quick calculations or easy obtained data.</p>\n'}, {'answer_id': 71708, 'author': 'Skizz', 'author_id': 1898, 'author_profile': 'https://Stackoverflow.com/users/1898', 'pm_score': -1, 'selected': False, 'text': '<p>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:</p>\n\n<pre><code>class Bob\n{\n  void FuncA ()\n  {\n    Foo.Bar foobar;\n  }\n}\n</code></pre>\n\n<p>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.</p>\n\n<p><a href="http://www.javaworld.com/javaworld/javaqa/1999-08/01-qa-static2.html" rel="nofollow noreferrer">A description of class types in Java</a>.</p>\n'}, {'answer_id': 74400, 'author': 'Jesse Glick', 'author_id': 12916, 'author_profile': 'https://Stackoverflow.com/users/12916', 'pm_score': 9, 'selected': True, 'text': '<p>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.</p>\n\n<p>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!)</p>\n\n<p>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:</p>\n\n<pre><code>public class Foo {\n    public interface Bar {\n        void callback();\n    }\n    public static void registerCallback(Bar bar) {...}\n}\n// ...elsewhere...\nFoo.registerCallback(new Foo.Bar() {\n    public void callback() {...}\n});\n</code></pre>\n'}, {'answer_id': 81569, 'author': 'Bas Leijdekkers', 'author_id': 4510, 'author_profile': 'https://Stackoverflow.com/users/4510', 'pm_score': 4, 'selected': False, 'text': '<p>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 <a href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.5.1" rel="nofollow noreferrer">8.5.1. Static Member Type Declarations</a></p>\n'}, {'answer_id': 209158, 'author': 'ColinD', 'author_id': 13792, 'author_profile': 'https://Stackoverflow.com/users/13792', 'pm_score': 6, 'selected': False, 'text': "<p>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 <code>Listener</code>. If you had a class <code>Foo</code> and you wanted other classes to be able to listen for events on it, you could declare an interface named <code>FooListener</code>, which is ok, but it would probably be more clear to declare a nested interface and have those other classes implement <code>Foo.Listener</code> (a nested class <code>Foo.Event</code> isn't bad along with this).</p>\n"}, {'answer_id': 14354017, 'author': 'user1982892', 'author_id': 1982892, 'author_profile': 'https://Stackoverflow.com/users/1982892', 'pm_score': 3, 'selected': False, 'text': "<p>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 <b>any</b> 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, <i> an inner interface enables the construction of custom types and improves their encapsulation</i>.</p>\n\n<pre><code>class ConcreteA implements A {\n :\n}\n\nclass ConcreteB implements B {\n :\n}\n\nclass ConcreteC implements C {\n :\n}\n\nclass Zoo implements A, C {\n :\n}\n\nclass DoSomethingAlready {\n  interface AC extends A, C { }\n\n  private final AC ac;\n\n  DoSomethingAlready(AC ac) {\n    this.ac = ac;\n  }\n}\n</code></pre>\n"}, {'answer_id': 30240388, 'author': 'Danylo Volokh', 'author_id': 1748464, 'author_profile': 'https://Stackoverflow.com/users/1748464', 'pm_score': 0, 'selected': False, 'text': '<p>If you will change class Foo into interface Foo the "public" keyword in the above example will be also redundant as well because </p>\n\n<blockquote>\n  <p>interface defined inside another interface will <strong>implicitly public\n  static.</strong></p>\n</blockquote>\n'}, {'answer_id': 31952505, 'author': 'Pindatjuh', 'author_id': 252704, 'author_profile': 'https://Stackoverflow.com/users/252704', 'pm_score': 0, 'selected': False, 'text': '<p>In 1998, Philip Wadler suggested a difference between static interfaces and non-static interfaces.</p>\n\n<blockquote>\n  <p>So far as I can see, the only difference in making an\n  interface non-static is that it can now include non-static inner\n  classes; so the change would not render invalid any existing Java\n  programs.</p>\n</blockquote>\n\n<p>For example, he proposed a solution to the <a href="https://en.wikipedia.org/wiki/Expression_problem" rel="nofollow">Expression Problem</a>, 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.</p>\n\n<p>An example of the difference between static and non-static nested interfaces can be seen in <a href="http://homepages.inf.ed.ac.uk/wadler/papers/expression/expression.txt" rel="nofollow">his sample code</a>:</p>\n\n<pre><code>// This code does NOT compile\nclass LangF&lt;This extends LangF&lt;This&gt;&gt; {\n    interface Visitor&lt;R&gt; {\n        public R forNum(int n);\n    }\n\n    interface Exp {\n        // since Exp is non-static, it can refer to the type bound to This\n        public &lt;R&gt; R visit(This.Visitor&lt;R&gt; v);\n    }\n}\n</code></pre>\n\n<p>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.</p>\n'}]
['https://Stackoverflow.com/questions/71625', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1870/']