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);
}