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.
-
1Because [**he**](http://en.wikipedia.org/wiki/James_Gosling) said so! – jjnguy Nov 15 '10 at 15:12
-
1possible duplicate of [Why can't I declare static methods in an interface?](http://stackoverflow.com/questions/21817/why-cant-i-declare-static-methods-in-an-interface) – Wim Coenen Nov 15 '10 at 15:52
6 Answers
Because an interface describes what. It doesn't describe how.

- 387,369
- 54
- 702
- 768

- 19,079
- 3
- 51
- 79
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.

- 113,398
- 19
- 180
- 268
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).

- 14,424
- 7
- 37
- 41
-
1Multiple 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
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.

- 4,699
- 10
- 43
- 56
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.

- 42,730
- 18
- 77
- 103
-
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.

- 87,612
- 17
- 125
- 175