• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why can't declare a Static method in Interface?

 
Mihir Patel
Greenhorn
Posts: 18
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 349
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . 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
Posts: 18
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anything other than constants - which is effectively what static, final variables are, are implementation details. And interfaces don't contain any implementation.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic