• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why no static methods in interface

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Why static methods are not allowed in interfaces?

Thanks
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rajiv,

this question discussed many times here. Please, SearchFirst
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seetharaman Venkatasamy wrote:this question discussed many times here. Please, SearchFirst


@Rajiv: And when you do, you may be surprised by the results.

Let me turn the question around for you: Imagine, for a moment, that you're a designer of the Java language; how would YOU implement static methods for interfaces?

And don''t just machine-gun back a reply; think about it.

There are several of us who think that, in theory, it would be no bad thing; but there are a lot of layers to consider - not the least of which is the behaviour of a class that implements two interfaces, both of which define the same static method.

Winston
 
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Methods declared in an interface are implicitly abstract methods. so, by definition, they do not define the implementation of the method.

 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:Methods declared in an interface are implicitly abstract methods. so, by definition, they do not define the implementation of the method.


Actually, it goes a bit further than that, since a method cannot be both static AND abstract.

I think (or I hope) that Rajiv's question is a general one, and it does have some merit (there's no particular logical reason I know of why an interface shouldn't be able to define a static method - indeed I believe some OO languages do allow it); it's just that you have a lot of other things to consider.

Winston
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajiv Rai wrote:Why static methods are not allowed in interfaces?


And, after all this discussion, the fact is that there IS a way of defining static methods for an interface; just not directly. Viz:and then you'd call it with:
Someclass result = MyInterface.Utils.doSomething({whatever});

Winston
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:I think (or I hope) that Rajiv's question is a general one, and it does have some merit (there's no particular logical reason I know of why an interface shouldn't be able to define a static method - indeed I believe some OO languages do allow it); it's just that you have a lot of other things to consider.



In other words, the answer is the standard answer: You can't do X in Java because the language designers decided not to let you do X.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're looking for a reason why one might choose not to allow it - I'd say that the point of an interface is to define a contract. And I'm having difficulty thinking of how a static method has a role to play in that.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:If you're looking for a reason why one might choose not to allow it - I'd say that the point of an interface is to define a contract. And I'm having difficulty thinking of how a static method has a role to play in that.


Hmmm. And I always though that it was to define a type; and I don't see anything intrinsically wrong with associating a static method with a type.

I suspect Paul's probably right: it's one of those "because I said so" situations. I also reckon it probably has a lot to do with keeping things as simple as possible, which was one of Java's original foundation stones.

Winston
 
Rajiv Rai
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:


There are several of us who think that, in theory, it would be no bad thing; but there are a lot of layers to consider - not the least of which is the behaviour of a class that implements two interfaces, both of which define the same static method.

Winston




The above problem applies to normal non static methods also i guess.. A class can implement 2 interfaces both of which can have same method
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajiv Rai wrote:
The above problem applies to normal non static methods also i guess.. A class can implement 2 interfaces both of which can have same method



Yes, this is true.
 
Rajiv Rai
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So , is there any specific disadvantage or design consideration that interfaces
were not allowed to have static methods
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajiv Rai wrote:So , is there any specific disadvantage or design consideration that interfaces
were not allowed to have static methods



The reason they can't have static methods is that interfaces are used for runtime polymorphism. An interface's methods are all abstract, by design and definition. That's core to the whole purpose of interfaces. In Java, static methods cannot be abstract. There's no overriding of static methods, no runtime polymorphism. So they have no place in an interface.

Now, you may go on to ask why Java doesn't allow runtime polymorphism of static methods, and the answer to that has already been given: Because that's what the designers chose. We can't know for sure why without digging up some of their old white papers, but, as has already been pointed out, simplicity is a good candidate.
 
Rajiv Rai
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah.. Thank for the replies.. I will go through them and try to understand it
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic