Why `private static` field is not allowed in Java 8 interface?

后端 未结 5 1993
野的像风
野的像风 2021-01-11 11:42

When I\'m trying to compile the following code

public interface SomeInterface{
    private static Logger logger = Logger.getLogger();

    public default voi         


        
5条回答
  •  清歌不尽
    2021-01-11 12:18

    In the pre-Java-8 view of the world, interfaces were purely for interface contracts, and private members exist purely for implementation, so this restriction was completely sensible.

    In the post-Java-8 world, where interfaces can carry behavior (but not state), it starts to be reasonable to ask whether other features of classes should be applied to interfaces as well. (However, just because something might be "reasonable" doesn't mean it must be supported; there is often more than one reasonable way to construct the world.)

    In Java 9, private methods in interfaces will be supported.

提交回复
热议问题