This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Why can't declare a Static method in Interface? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why can Watch "Why can New topic
Author

Why can't declare a Static method in Interface?

Mihir Patel
Greenhorn

Joined: Apr 26, 2011
Posts: 18

Hi,

1) Please give me a reason why we can't declare a static method in Interfaces. and,
2) Why member variables in interface are Static and Final?

Thanks..
Ananth Chellathurai
Ranch Hand

Joined: Nov 21, 2007
Posts: 349

Hi Mihir,
Static methods must have bodies. The methods in interfaces don't have bodies, and are thus not static methods. They are actually public abstract methods.


Ananth Chellathurai [Walk on software]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38430
    
  23
. . . and public abstract methods must be implemented in a subclass, which you can only do for instance methods, so they can't be static.
Mihir Patel
Greenhorn

Joined: Apr 26, 2011
Posts: 18

Thanks Ananth and Ritchi...

Please someone answer me for the below 2nd question in this post.

2) Why member variables in interface are Static and Final?

Thanks.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Mihir Patel wrote:
Why member variables in interface are Static and Final?

Interface cant be instantiated, so variable must assigned to a value in static context and it is final because, variables in interface are constant!
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4367
    
    8

Anything other than constants - which is effectively what static, final variables are, are implementation details. And interfaces don't contain any implementation.
 
Consider Paul's rocket mass heater.
 
subject: Why can't declare a Static method in Interface?