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

后端 未结 5 1943
野的像风
野的像风 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:21

    The goal of interface is to define something implemented by other classes. A private field does not define anything as it is not visible outside the interface. Hence it does not make any sense in this construct. It may be some hacks how to use it (maybe from interface inner classes) but would not look like a good design anyway.

    If you actually implement part of the functionality, use abstract class instead.

提交回复
热议问题