java接口main_接口内部的主要方法(java-8)

这个问题已经在这里有了答案:            >            Why are interfaces in Java 8 allowed to have the main method?                                    5个

由于java-8允许在接口内部使用静态方法,因此我决定检查一下,这样简单的编译程序能否成功运行:

public interface Test {

static void main(String[] args) {

System.out.println("I'm ok!");

}

}

令人惊讶的(对我而言)它运行完美.

我感到困惑的是,在JVM规范术语中,类和接口具有不同的语义和用法.在这种情况下,我担心这种行为是否确实符合JVMS?因为实际上JVMS Ch. 5.2.谈论的是类,而不是类或接口:

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:

我知道,该接口本身就是一个类.但是我说的是JVM,其中类和接口通常具有不同的语义. 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是类还是接口,都遵循相同的过程.

Java语言规范在描述虚拟机启动方面同样不一致,在某一点使用“类”,而在另一处使用“类或接口”.

无论如何,将main方法作为接口的一部分应该可以很好地工作.

标签:java-8,jvm,specifications,java

来源: https://codeday.me/bug/20191119/2034245.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值