Java lazy loading of enum instances

前端 未结 2 1352
夕颜
夕颜 2020-12-18 06:02

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. I

2条回答
  •  佛祖请我去吃肉
    2020-12-18 06:15

    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.

提交回复
热议问题