为什么在 Java 8 中托管 main 的接口不必是 public?

Why does an interface hosting main in Java 8 not have to be public?

为什么下面的代码在Java 8+eclipse中编译运行成功?

package package1;
interface A  
{
    static void main(String[] args) {
        System.out.println("Hi");
    }
}

如果 A 更改为 class,运行-time 要求它是 public class,但接口不是这样。这似乎不一致。

编辑: 我在发布问题时所做的上述陈述是错误。 (我一定是累了,看错了错误)。 Java 不要求 class 托管主体为 public,仅要求方法。 但是,托管 main 的类型不必是 public,而 main 方法必须是

,这有点不一致。

If A is changed to a class, the run-time requires it to be a public class.

不,不是。它要求 方法 是 public,接口中的方法已经是 public.

but not so for an interface.

不是这样。

This seems inconsistent.

不是。您误读了错误消息。

接口及其字段和方法总是public。

如果 A 是包含 main 方法的 class,则 A 必须是 public。这是因为 main 方法总是

public static void main(String[] args)

在 java 1.8 之前的静态方法是不允许的。

所有方法都是默认的 public,因此您不必显式使用关键字。

interface myInterface {
public void show();
//same as above
 void show();
}

从java8开始,接口也可以有static个方法。

因此您可以使用 static 方法但不需要 public 关键字

interface myInterface {
static void main(String[] args) {}
void show();
}