0

Since Java 8 we can have static interfaces, like this:

public interface MyInterface {
  public static String getData(String data){
    return null;
  }
  public static boolean isValidData(String data){
    return false;
  }
}

But what's the case when it makes sense to have an interface for static methods?

kecso
  • 2,387
  • 2
  • 18
  • 29
  • 3
    Your question is upside down. It is not about when having an interface for static methods would make sense, but about when static methods for an interface would make sense. – Andreas Jun 20 '16 at 16:48
  • 2
    “we can have static interfaces” is nonsense. A “static interface” is an interface that is a member type of another class like `class A { static interface B { } }` and *all* member interfaces are `static`, even if you omit the `static` keyword. This has nothing to do with the possibility of declaring `static` methods in an interface. You could always declare `static` fields in an interface, that also never made an `interface` a “static interface”… – Holger Jun 20 '16 at 17:39
  • 1
    And declaring an interface containing static methods only, *never* makes sense. – Holger Jun 20 '16 at 17:40
  • Yes the question title is misleading, what I meant: What's the benefit (is there's any) of using an interface for an implementation which contain only static methods. So the implementation implies that the interface will have static methods. It seems the answer is no. Thanks for the hints. – kecso Jun 20 '16 at 18:37
  • 2
    The benefit is that it allows interfaces to put static helper methods (such as factory methods, decorators, combinators, etc) in the interface itself, rather than having to have a less-discoverable "sidecar" class like "Collections" to put the static methods. (This is of significant value to library developers, probably less so to application developers.) – Brian Goetz Jun 20 '16 at 19:52

0 Answers0