Why are interfaces in Java 8 allowed to have the main method?

前端 未结 5 1044
北荒
北荒 2020-11-29 06:10

Why are interfaces allowed to have a main method in Java 8?

As stated in below code it works fine and produces output properly.

public i         


        
5条回答
  •  我在风中等你
    2020-11-29 07:01

    I second the answer of @jb-nizet. There is no "desparate need" for this, but it removes an unnecessary restriction. E.g. one example is, that you can now declare a factory method within the interface:

     public interface SomeService {
    
       public static SomeService getInstance() {
         // e.g. resolve via service provider interface
       }
    
       ...
    
     }
    

    Before Java 8 we needed always a separate factory class. One favorite example is the google app engine API.

提交回复
热议问题