aspose file tools*
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
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: 39393
    
  28
. . . 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: 4420
    
    8

Anything other than constants - which is effectively what static, final variables are, are implementation details. And interfaces don't contain any implementation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why can't declare a Static method in Interface?