嵌套类是否在接口内隐式静态且最终? [英] Are nested classes inside an interface implicitly static and final?

查看:161
本文介绍了嵌套类是否在接口内隐式静态且最终?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

接口内的所有变量都是public static和final。

All variables inside of interface are public static and final.

因此,如果我在接口中声明一个嵌套类,它是否也会变为静态和最终?

So if I declare a nested class inside an interface will it also become static and final?

Interface I
{
    class c
    {
        static void m(){ }
    }
}


推荐答案

让我们找出。让我们创建如下结构:

Lets find out. Lets create structure like:

interface Interface{
    class Foo{}
}

现在我们可以测试:

System.out.println("static: " + Modifier.isStatic(Interface.Foo.class.getModifiers()));
System.out.println("final: " + Modifier.isFinal(Interface.Foo.class.getModifiers()));

打印:

static: true
final: false

所以嵌套类是隐式静态的,但不是最终的。

So nested classes are implicitly static, but not final.

我们也可以通过将类扩展Foo {} 添加到我们的界面来确认它。 final 类无法扩展,但由于此类代码编译正常,因此 Foo 不是最终的。

We can confirm it also by adding class Bar extends Foo{} to our interface. final classes can't be extended but since such code compiles fine it means Foo is not final.

这篇关于嵌套类是否在接口内隐式静态且最终?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆