Search code examples
javainterfacestaticjava-8early-binding

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.


Solution

  • 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.