Re: Separate interface and implemenation problem..

Steven Simpson wrote:
I've not seen a compelling reason for static methods in an interface yet.

I wonder if a statically implemented interface would suit better in some
such cases:

// Not real code
class MyClass implements static Runnable {
  public static void run() { ... }


But that doesn't make Java sense. Static methods don't implement or
override static methods of supertypes, nor can static methods delegate
their action to subtype static methods. In no meaningful sense in
Java, at this time, does a static method in a subtype equate to a
static method in a supertype. Instead, one hides the other. JLS
If a class declares a static method m, then the declaration m is said
to hide any method m', where the signature of m is a subsignature (§8.4..2)
of the signature of m', in the superclasses and superinterfaces of the
class that would otherwise be accessible to code in the class.

The real question to ask is just what benefit one would expect from a
static method in an interface that isn't already available from static
methods in classes. Assuming there is such a benefit, and I can't
think of one, the next question is whether it's worth the distortion
of Java's semantics to make it work.



Relevant Pages

  • Re: Static methods verus Instance methods
    ... In my opinion, since static methods are not polymorphic in Java, you can't conveniently change implementations or code to an interface. ... public void largeExpensiveFileIO(File f) {//... ...
  • Re: COM and registration question
    ... > I'm assuming you are implementing a COM interface on a .NET type. ... > this is the case, you can create two static methods (one for registering, ... > component in the registry. ...
  • Re: static methods in interfaces
    ... I wanted to have a some static methods in an interface, ... Static methods declared in the interface would be ideal, ... if (shape instanceof Square) { ... Square square = shape; ...
  • Re: testing a dependency on a static function.
    ... "don't use static methods like that." ... does that mean that the proper way to have testable code for this ... example is to create an interface, create a concrete class that ... implements that interface, ...
  • Re: Java Collections List : Converting from List > to List
    ... My discussion isn't assuming any new Java features, simply providing static methods to execute with existing Java interfaces. ... Those static methods could explicitly check for, casting to and calling if available, the more efficient implementations for such methods, but would always have a fallback implementation guaranteed to work with the original Iterable interface. ...