Why would a static nested interface be used in Java? -
i have found static nested interface in our code-base.
class foo { public static interface bar { /* snip */ } /* snip */ }
i have never seen before. original developer out of reach. therefore have ask so:
what semantics behind static interface? change, if remove static
? why this?
the static keyword in above example redundant (a nested interface automatically "static") , can removed no effect on semantics; recommend removed. same goes "public" on interface methods , "public final" on interface fields - modifiers redundant , add clutter source code.
either way, developer declaring interface named foo.bar. there no further association enclosing class, except code cannot access foo not able access foo.bar either. (from source code - bytecode or reflection can access foo.bar if foo package-private!)
it acceptable style create nested interface way if expect used outer class, not create new top-level name. example:
public class foo { public interface bar { void callback(); } public static void registercallback(bar bar) {...} } // ...elsewhere... foo.registercallback(new foo.bar() { public void callback() {...} });
Comments
Post a Comment