enum中静态成员初始化往往会遇到问题,如下图:

enum的静态成员的初始化的问题及解决办法

上图文章地址

https://www.iteye.com/blog/rednaxelafx-460981

解决办法:

1.

 private static Map<Integer, OperationId> map = new HashMap<Integer, OperationId>();  

 static {  

        for (OperationIdp : OperationId.values()) {  

            OperationId.map.put(p.value, p);  

        }  

   }  

2. 

 private static HashMap<Integer, OperationId> getMappings() {
        if (mappings == null) {
            synchronized (OperationId.class) {
                if (mappings == null) {
                    mappings = new HashMap<>();
                }
            }
        }
        return mappings;
    }

  private OperationId(int value) {
        intValue = value;
        getMappings().put(value, this);
    }

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
  • 2021-12-25
  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
  • 2021-08-09
猜你喜欢
  • 2021-06-10
  • 2022-12-23
  • 2021-10-05
  • 2022-12-23
  • 2021-10-10
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案