class - What is the purpose of a static method in interface from Java 8? -


why static methods supported java 8? difference between 2 lines in main method in below code?

package sample; public class {     public static void dosomething()     {         system.out.println("make something!");     } }  public interface {     public static void dosomething()     {         system.out.println("make something!");     } }  public class b {     public static void main(string[] args) {         a.dosomething(); //difference between         i.dosomething(); //and     } } 

as can see above, not implemented in b. purpose serve have static method in interface when can write same static method in class , call it? introduced other purpose modularity. , modularity, mean following:

public interface singable {     public void sing();     public static string getdefaultscale()     {         return "a minor";     } } 

just put methods together.

in past, if had interface foo , wanted group interface-related utils or factory methods, need create separate utils class fooutils , store there.

those classes not have in common other name , additionally, utils class need made final , have private constructor forbid unwanted usage.

now, interface static methods, can keep in 1 place without creating additional classes.

it's important not forget practices , not throw mindlessly 1 interface class - pointed out in this answer


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -