When I'm trying to compile the following code
public interface SomeInterface{
private static Logger logger = Logger.getLogger();
public default void someMethod(){
logger.info("someMethod: default implementation");
}
}
I get an error
Illegal modifier for the interface field SomeInterface.logger; only public, static & final are permitted
When I delete private
modifier, code compiles, but I don't want other classes from the package to see this field.
Why Java doesn't allow me to do such thing when it actually does make sense?
Why? I don't know
---defender methods were the prerequisite to the effective introduction of the Streams API to the JDK. Many interfaces needed extension and without defenders that would be impossible as it would break Java's serious promise of backward compatibility. – Mcelrath