Force Initialization of an enumerated type in Java

后端 未结 4 1236
星月不相逢
星月不相逢 2021-02-20 01:46

I am attempting to find a way to force Java to load/initialize an enumerated type (which is nested within a class that contains a static Map).

This is important to me be

4条回答
  •  梦如初夏
    2021-02-20 01:50

    Can't you just put initialization of the map in the static initializer of the Enum type?

    public enum SomeEnum
    {
        Member1, Member2, Member3 ...
    
        private static Map map = ...;
        static 
        {
            ...populate map...
        }
        ...
    

    EDIT: It appears that the issue was that the Enum member definitions need to come first. I guess I just glossed over this. I fixed the example.

提交回复
热议问题