Take the 2-minute tour ×

I'm just curious, wouldn't it be more convinient to allow interfaces to contain implementations of static methods? Such methods could contain short commonly used(by this interface implementors) logic.

share|improve this question
Because he said so! –  jjnguy Nov 15 '10 at 15:12
face.......palm –  Brian Driscoll Nov 15 '10 at 15:13
possible duplicate of Why can't I declare static methods in an interface? –  Wim Coenen Nov 15 '10 at 15:52

7 Answers 7

Because an interface describes what. It doesn't describe how.

share|improve this answer

It's the abstract class or regular class which should implement something. Interfaces are not supposed to have any implementations, but they contain the interface of communicating. So static methods are not allowed.

share|improve this answer
I know, I just ask why? –  Vladimir Nov 15 '10 at 15:17
to abstract the client code from any implementation details. Client should not be bothered of how the things get done. –  Vladimir Ivanov Nov 15 '10 at 15:18

An interface is a special abstract class with all abstract methods.

You can feel free to create an abstract class of your own that contains (non-abstract) static methods, but then you can only inherit from one of them.

Better yet, create a separate helper class with your static methods.

share|improve this answer

An interface is a contract. It says what an implementing object will have (at minimum), but that's all. It says "this house will have a door, a window, and a chimney".

An abstract class is more like a prefab house. It's not complete (you have to add your own siding, for example) but it has parts already there (there is a space for the door, but the whole fireplace is already setup.

The problem with giving code in interfaces is multiple inheritance. Java doesn't allow it. You can have a class implement many interfaces, because interfaces only promise there will be a method with a given signature.

If interfaces held code, then you could implement 3 of them, each with a method body for myUsefulFunction(String thing)... and now you don't know which one gets called.

That's why abstract classes can have method bodys (because you can only extend one class), but interfaces can't (because you can implement multiple interfaces).

share|improve this answer
Multiple inheritance would not be a problem in the case of static methods, because they are resolved at compile time. Thus it would be trivial to throw a compile error when a static call was ambiguous. –  ILMTitan Nov 15 '10 at 18:17

If you really want to add (hide) some logic inside an interface, you may consider adding an inner class (Note: never do it, this just shows what is possible from a pure technical perspective):

public interface Person {
  public String getFirstName();
  public String getLastName();
  public class Util {
    public String getName(Person person) {
      return person.getFirstName() + " " + person.getLastName();

If you use this, it "feels" a bit like having static method code in the interface:

String fullName = Person.Util.getName(this);

As I said - it's pure technically and I don't see any reason to actually do it. A static method can be located in any class, no need to add it to an interface.

share|improve this answer

I agree that a static method doesn't make sense in an interface. But i don't understand why java allows static members in an interface. Seems a bit inconsistent.

share|improve this answer

The answer is already explained neatly here
Just read the first accepted answer.

share|improve this answer
that answer is unaccepted and not enterly correct. See the accepted answer on the same question. –  vidstige Feb 21 '11 at 20:18
@vidstige : oh. yeah. Editing and will post correct one. :) –  99tm Feb 21 '11 at 20:19
@vidstige : Why still downvote ? –  99tm Feb 21 '11 at 20:25
sweet. just changed it. –  vidstige Feb 21 '11 at 20:26

Your Answer


