Why would anyone define a static method in an interface in java 1.8? [on hold]

  • A+
Category:Languages

Why would any one define a static method in an interface in JAVA 1.8?

I need to know the different examples/usecases/requirements where static method would come handy.

How can a static method in an interface would be beneficial for a developer?

 


There are many use cases for static methods in interfaces. The most common by far, though, is factory methods that have made it easy to create instances of their respective interfaces.

Consider these examples:

Comparator.comparing List.of Set.of Stream.of, Stream.generate 

Without these factory methods in interfaces, there would be too many constructor calls in disparate implementation classes.

Some of these examples were only added with version 9 of Java, but the feature opened many possibilities.

These static methods are also used for different purposes. It's now possible to have a method like Collections.sort implemented in the right place, Collection.sort, thus avoiding unnecessary extra classes.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: