This week's book giveaway is in the Java in General forum.
We're giving away four copies of Java by Comparison (eBook) and have Simon Harrer, Jörg Lenhard, Linus Dietz on-line!
See this thread for details.
Win a copy of Java by Comparison (eBook) this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java 8 - static method in interface  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Kathy/Bert

What do you think about static methods in interfaces?
They are great in many situations
But is it not a little odd?

Thanks,
Wojtek
 
Enthuware Software Support
Rancher
Posts: 4158
34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Wojtek Klem wrote:Hello Kathy/Bert

What do you think about static methods in interfaces?
They are great in many situations
But is it not a little odd?


Not if you consider that static fields have been allowed since the beginning. I would say they should have allowed static methods and static fields in interfaces since the beginning and should not have allowed static methods and fields in classes at all, but I am sure they had reasons

Default methods, otoh, is altogether a different story
 
Saloon Keeper
Posts: 8457
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure why everyone is so opposed to default methods. I think they're good for simulating traits, which is a feature Java sorely needed.
 
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Anilprem wrote:and should not have allowed static methods and fields in classes at all


Why

Paul Anilprem wrote:Default methods, otoh, is altogether a different story


I like them! You can write something like this (and you don't need to create an additional abstract class having nothing but that default method implementation)Works like a charm!
 
Paul Anilprem
Enthuware Software Support
Rancher
Posts: 4158
34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:I'm not sure why everyone is so opposed to default methods. I think they're good for simulating traits, which is a feature Java sorely needed.


I am not really opposed to default methods. But they are an after thought. I find the inheritance and override rules for default and static methods too complicated.
 
Paul Anilprem
Enthuware Software Support
Rancher
Posts: 4158
34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roel De Nijs wrote:

Paul Anilprem wrote:and should not have allowed static methods and fields in classes at all


Why


They just complicate things and have little design value. If you think about it, what are static methods for? They are mostly utility methods. If you don't need an object to invoke these methods, why should they be in a class at all? They complicate the rules of inheritance unnecessarily. You can invoke a static method defined in a super class using the subclass name but they are not "inherited". This is ugly, imho. One of the design goals of Java has been to reduce ways that allow programmers to write bad code. This is one such thing that could have been avoided.

 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Anilprem wrote:One of the design goals of Java has been to reduce ways that allow programmers to write bad code. This is one such thing that could have been avoided.


True! But on the other hand there is something like the Constant Interface Anti-Pattern. And what about defining private constants in your class?
 
Paul Anilprem
Enthuware Software Support
Rancher
Posts: 4158
34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roel De Nijs wrote:

Paul Anilprem wrote:One of the design goals of Java has been to reduce ways that allow programmers to write bad code. This is one such thing that could have been avoided.


True! But on the other hand there is something like the Constant Interface Anti-Pattern. And what about defining private constants in your class?


An interface is not the perfect location for static methods but I think it is a better location than a class.

The Constant Interface Anti-Pattern is an anti-pattern because it is an abuse of "implements". I don't think there is anything wrong with constants in an interface if you don't make your class implement that interface just so that you could directly use the constants. private constants in a class are also ok. private anything is not too bad, I think. private keeps the stink from leaking out I just don't like static mixed with class. Doesn't feel right.
 
author
Sheriff
Posts: 8955
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm on thin ice a bit here but...

I think we can learn some good lessons from FP while at the same time not having to be purely FP. One such lesson is to remove state from as many classes as possible, and to corral (hey, a rancher term!), state into specific parts of your app. To that end, I think empowering interfaces as Java 8 did can help with such redesign. I would agree however that it's harder to talk about things like inheritance now. 
 
Ever since I found this suit I've felt strange new needs. And a tiny ad:
Thread Boost - a very different sort of advertising
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!