Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why can't interface methods be static?

 
Rachel Glenn
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that interface methods are implicitly public and abstract. Interface variables must always be public, static and final. Is there a specific reason interfaces can't have static methods?
 
Leena Diwan
Ranch Hand
Posts: 351
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static means 'one per class'
Usually we mark a method 'static' in a class and can call it without having to make an instance of that class.

In case of interfaces, where is the body of the method?
What will it execute, even if it is allowed.
If the methods of an interface are implicitly 'abstract' it means that the method body is not there.

HTH
 
Leena Diwan
Ranch Hand
Posts: 351
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check some discussions on similar lines here
 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Rachel,

All methods of interface of implicitely abstract, whcih should be overridden by the implementing class. As the static method can not be overridden, interface does not allow creation of static methods.

If you want to have some static method in your parent class, you can think of considering abstract class as the parent rathen than interface.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic