Java-8允许在接口内部定义静态方法,但仅通过接口名称限制它的调用:
9.4:接口可以声明静态方法,这些方法在不引用特定对象的情况下被调用.
例如:
interface X {
static void y() {
}
}
...
X x = new X() {};
x.y();
Run Code Online (Sandbox Code Playgroud)
导致错误:
error: illegal static interface method call
x.y();
^
the receiver expression should be replaced with the type qualifier 'X'
Run Code Online (Sandbox Code Playgroud)
通常在JLS中,这种禁令有一个解释.在这种情况下,我没有发现任何详细的信息.所以我正在寻找对此规则的全面或权威解释:为什么禁止通过特定对象引用调用静态方法?它打破了什么?