This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why can't interfaces define a static method

 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why can't interfaces define static methods?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The simple answer to all such questions is "because the Java Language Specification says so".

In many such questions, it would be possible to imagine how the feature under discussion might have been allowed. The language designers had to make judgements about what features would be useful, understandable, easy to implement in compilers/JVMs, and encourage good programming. Static methods in interfaces would fall down on several of those criteria.
 
Ådne Brunborg
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another answer is that they can. But such interfaces are called "abstract classes" and not "Interfaces" . Abstract classes and Interfaces are very similar in nature - they are both abstract (i.e. cannot be instantiated), and defines method signatures.

The difference is mainly that abstract classes can define method implementations as well - including static methods.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All methods in an interface are abstract. Static methods cannot be abstract, because they aren't polymorphic.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all of you for your replies. It was helpful.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic