以下情况可以通过编译
Class A { static void staticMethod() { println(); } } Class B extends A { } B.staticMethod();
而以下情况则不可以
interface A { static void staticMethod() { println(); } } Class B implements A { } B.staticMethod();
这里会提示错误
static method may be invoked on containing interface class only.
这是因为
在Java 8中,在接口中添加静态方法带来了一个限制 :这些方法不能由实现它的类继承。
这样做是有道理的,因为一个类可以实现多个接口。如果2个接口具有相同的静态方法,它们都将被继承,编译器就不知道要调用哪个接口。