I am just wondering if there is any difference for object initialisation in jvm, based on given the code blocks. If so which one is the correct way of doing?
enum FlagType {
NOT_DEFINED(0, null),
TEST(1, Constants.TEST),
PROD(5, Constants.PROD),
QA(8, Constants.QA);
private final Integer id;
private final String value;
private static final Map<Integer, FlagType> cacheValueById;
static {
Map<Integer, FlagType> valueById = Arrays.stream(FlagType.values())
.collect(Collectors.toMap(FlagType::getId,
Function.identity()));
cacheValueById = valueById;
}
}
enum FlagType {
NOT_DEFINED(0, null),
TEST(1, Constants.TEST),
PROD(5, Constants.PROD),
QA(8, Constants.QA);
private final Integer id;
private final String value;
private static final Map<Integer, FlagType> cacheValueById;
static {
cacheValueById = Arrays.stream(FlagType.values())
.collect(Collectors.toMap(FlagType::getId,
Function.identity()));
}
}
java-*tags when your problem is specific to one of those versions. Since you added so many, and didn't say anything to the contrary, I've assumed it's not specific to any version.enumtypes lack constructors. But these additions were irrelevant to your question anyway. The original code snippets were already sufficient. Ironically, the only issue, the inconsistency between “FlagType” and “Ctype” is still present…