I have an enum class with several key. A number of keys should have the same string value. Sonar yells at me to replace the same string values with a constant value.
For example:
public enum MESSAGE_TYPES {
KEY1("Val1"),
KEY2("Val2"),
KEY3("Val3"),
KEY4("Val2"),
KEY5("Val4"),
KEY6("Val2"),
//etc.
}
So, sonar wants me to :
Define a constant instead of duplicating this literal...
for "Val2" since it is defined 3 or more times. How can I achieve that?
Auto extracting the value to a constant puts it right after the enum keys, but then the value of the enum key doesn't recognize it of course. So, I tried to put it on top of the enum class
public enum MESSAGE_TYPES {
private static final String VAL2 = "Val2";
KEY1("Val1"),
KEY2(VAL2),
...
}
and so I get "Syntax error" errors on the line.
Please advise.
Thanks!