Why was this(Allowance of Static Methods definition) added in java development kit version 1.8 ?
Whats the use of adding this functionality ?
Why was it needed ?
I know those methods can be called with Interface.method() in case of any class which implements this interface !
Why did they need to add this :?
I mean if you can define static methods in interface its kind of similar to abstract class ? Isn't It ?
Correct me if i am wrong !!!!
I don't claim to be rite :)
Asked
Active
Viewed 737 times
0

Vaibhav Vaghela
- 57
- 5
-
See [here](http://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html).'In addition to default methods, you can define static methods in interfaces. (A static method is a method that is associated with the class in which it is defined rather than with any object. Every instance of the class shares its static methods.) This makes it easier for you to organize helper methods in your libraries' – Stefan Aug 28 '14 at 12:50
1 Answers
1
It wasn't really a need to have this. After all, we have survived since the beginning of Java without them. But it's a nice to have. It's quite frequent to have an interface (Collection, for example), with another class containing static utility methods dealing with this interface (Collections, for example).
Putting the utility methods in the interface makes finding these utility methods easier, and there is no reason not to allow static methods in interfaces, so why not allowing them.....
-
Well i get what you are trying to say. Some static methods of Collections can be used in any of the derived classes ! Which can come in handy sometimes ! if you can give example of any static methods in collections or interface which is frequently used . it would be delightful – Vaibhav Vaghela Aug 28 '14 at 12:57
-
1I find it a *design mistake* to allow static methods in interfaces (as all it does it encourages even more static methods, on "contracts" no less!) .. but I wasn't on the committee. Moral of the story: *it is what it is*, but that is what I say about most of Java. – user2864740 Aug 28 '14 at 12:57
-
@VaibhavVaghela: the static methods are not (only) for implementors of the interface. They're for users of the interface. Look at the classes Collections, Files, Paths. – JB Nizet Aug 28 '14 at 14:16