CodingRat
CodingRat

Reputation: 1944

Declaring static method is allowed in java what are the advantage of doing so

What is the advantage of having static method and default method introduced in java 8 as i found it will add complexity and ambiguity in your code. Please bring some light on this.

Upvotes: -2

Views: 118

Answers (2)

Marko Topolnik
Marko Topolnik

Reputation: 200158

The advantages are clear: static methods in the interface allow factories such as Stream.of to be placed right where they belong. Previously you would need a StreamUtil class or similar to hold them. Defender methods ("default") were an absolute must in order to introduce Stream-oriented goodness all around the Collections API, and are a very useful feature on their own, allowing free growth of API with convenience methods which only rely on other methods of the public API.

No complexity or ambiguity is added to your code, especially since the static methods are not inheritable.

Upvotes: 2

René Winkler
René Winkler

Reputation: 7058

The biggest adavantage of having default methods is that you can evolve an API for new features even it was already released without breaking the implementation of the users of the API.

Static methods make it possible to dispense with utility classes as the implementation can be written in the interface.

Upvotes: 1

Related Questions