接口内的主要方法 (java-8)
main method inside interface (java-8)
由于java-8允许接口内部的静态方法,我决定检查一下,这样简单的编译程序是否会成功运行:
public interface Test {
static void main(String[] args) {
System.out.println("I'm ok!");
}
}
令人惊讶的是(对我来说)它 运行 非常完美。
让我感到困惑的是,在 JVM 规范中,术语 class
和 interface
具有不同的语义和用法。在这种情况下,我担心这种行为是否真的符合 JVMS?因为实际上JVMS Ch. 5.2。谈论 class
,而不是 class or interface
:
The Java Virtual Machine starts up by creating an initial class, which
is specified in an implementation-dependent manner, using the
bootstrap class loader (§5.3.1). The Java Virtual Machine then links
the initial class, initializes it, and invokes the public class method
void main(String[]).
UPD:
我知道,那个界面本身就是一个class。但我说的是 JVM,其中 class
和 interface
通常具有不同的语义。 For example
The run-time constant pool for a class or interface is constructed
when the class or interface is created (§5.3) by the Java Virtual
Machine.
JVM 规范在这里的措辞有点松懈。如果你引用引用的section 5.3.1,它说:
The following steps are used to load and thereby create the nonarray class or interface C denoted by N using the bootstrap class loader.
重点是无论 C 是 class 还是接口,都遵循相同的过程。
Java 语言规范在描述虚拟机启动方面同样不一致,有时使用 "class",有时使用 "class or interface"。
无论如何,将 main
方法作为接口的一部分应该可以很好地工作。
由于java-8允许接口内部的静态方法,我决定检查一下,这样简单的编译程序是否会成功运行:
public interface Test {
static void main(String[] args) {
System.out.println("I'm ok!");
}
}
令人惊讶的是(对我来说)它 运行 非常完美。
让我感到困惑的是,在 JVM 规范中,术语 class
和 interface
具有不同的语义和用法。在这种情况下,我担心这种行为是否真的符合 JVMS?因为实际上JVMS Ch. 5.2。谈论 class
,而不是 class or interface
:
The Java Virtual Machine starts up by creating an initial class, which is specified in an implementation-dependent manner, using the bootstrap class loader (§5.3.1). The Java Virtual Machine then links the initial class, initializes it, and invokes the public class method void main(String[]).
UPD:
我知道,那个界面本身就是一个class。但我说的是 JVM,其中 class
和 interface
通常具有不同的语义。 For example
The run-time constant pool for a class or interface is constructed when the class or interface is created (§5.3) by the Java Virtual Machine.
JVM 规范在这里的措辞有点松懈。如果你引用引用的section 5.3.1,它说:
The following steps are used to load and thereby create the nonarray class or interface C denoted by N using the bootstrap class loader.
重点是无论 C 是 class 还是接口,都遵循相同的过程。
Java 语言规范在描述虚拟机启动方面同样不一致,有时使用 "class",有时使用 "class or interface"。
无论如何,将 main
方法作为接口的一部分应该可以很好地工作。