栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

枚举类型为构造函数的枚举类型中的静态块的执行顺序

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

枚举类型为构造函数的枚举类型中的静态块的执行顺序

我理解您的问题是:为什么可以保证在运行静态块之前会初始化枚举常量。答案在JLS中给出,具体示例在#8.9.2.1中给出,并带有以下说明:

静态初始化从上到下进行。

和枚举常量是隐式最终静态的,并在静态初始值设定项块之前声明。

编辑

行为与普通班级没有什么不同。下面的代码打印:

In constructor: PLUSPLUS == null MINUS == nullIn constructor: MINUSPLUS != null MINUS == nullIn static initialiserPLUS != null MINUS != nullIn constructor: after staticPLUS != null MINUS != nullpublic class Operation {    private final static Operation PLUS = new Operation("PLUS");    private final static Operation MINUS = new Operation("MINUS");    static {        System.out.println("In static initialiser");        System.out.print("PLUS = " + PLUS);        System.out.println("tMINUS = " + MINUS);    }    public Operation(String s) {        System.out.println("In constructor: " + s);        System.out.print("PLUS = " + PLUS);        System.out.println("tMINUS = " + MINUS);    }    public static void main(String[] args) {        Operation afterStatic = new Operation ("after static");    }    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/515179.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号