Java延迟加载枚举实例 [英] Java lazy loading of enum instances

查看:157
本文介绍了Java延迟加载枚举实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在枚举类型中有一堆枚举实例,并且如果我第一次访问它的一个实例,那么所有其余的实例也将同时被初始化。有没有办法只有在第一次访问枚举实例时才能初始化枚举实例?

解决方案

code> enum 枚举是类。第一次使用类,它由JVM加载,所有的静态初始化都完成了。设置枚举成员是一个静态初始化,所以它们都将被初始化。


If I have a bunch of enum instances in an enum type, and if I access an instance of it the first time, all of its remaining instances too are initialized at the same time. Is there any way to initialize an enum instance only when it's accessed the first time?

解决方案

Not without basically making it not an enum anymore. Enums are classes. The first time a class is used, it gets loaded by the JVM and all of its static initialization is done. Setting up the enum members is a static initialization, so they're all going to be initialized.

这篇关于Java延迟加载枚举实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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