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

前端 未结 5 1048
北荒
北荒 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 06:48

    From Brian Goetz's in the comments:

    I think you have the question backwards. The question is not "should main methods be allowed", it is "should we have explicitly disallowed main methods now that static methods are permissible in interfaces" (or, equivalently, exclude interface classes as targets for the java launcher.) This would have been adding extra rules just to exclude something some people perceive as weird (but not dangerous) -- and that's a losing game. You want to define as few new rules as you can reasonably get away with -- because otherwise you get mired in complexity.

提交回复
热议问题