0

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()));
   }
}
2
  • 3
    Please only use the 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. Commented Apr 14, 2020 at 10:46
  • 1
    Your enum types 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… Commented Apr 14, 2020 at 11:24

2 Answers 2

1

There is no practical difference, you're just making the code more verbose with the variable.

The difference between assigning a value to a variable and then assigning the variable to a field vs directly assigning to the field is not much different to assigning to a variable before returning that variable, vs just returning the value: the variable doesn't really add anything (unless you, say, need to suppress a warning or similar).

In fact, it detracts from the code because there is more "stuff" there, like the variable type and name. Do these add anything to a reader's understanding?

In this case, assigning to a variable first adds no value, so don't use it.

That said, the preferable approach is not to use a static initializer block at all. Just assign on the field declaration.

private static final Map<Integer, FlagType> cacheValueById = 
    Arrays.stream(FlagType.values())
        .collect(Collectors.toMap(FlagType::getId,
                Function.identity()));
Sign up to request clarification or add additional context in comments.

3 Comments

And you can avoid the array creation with EnumSet.allOf(FlagType.class).stream()
sorry, the question should have been like this
@FreeMan I don't see that your edit changes the question; it merely adds extra detail.
0

No differences you just have an intermediary local variable in first example(btw useless). Init block is thread safe because called by classloader thread.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.