Is there a way to define a constant Map
for the use in a switch statement? All my trials with static Maps from here, here and others were not successful. Why isn't the Map constant?
For the line case (kws.get(KEYWORD_NAME)):
I always get constant string expression required
error.
public class Demo {
public static final String KEYWORD_NAME = "Name";
public static final String KEYWORD_TYPE = "Type";
private static final Map<String, String> kws = Collections.unmodifiableMap(
new HashMap<String, String>() {{
put(KEYWORD_NAME, KEYWORD_NAME.toLowerCase());
put(KEYWORD_TYPE, KEYWORD_TYPE.toLowerCase());
}});
public static void parse(String kw){
switch(kw){
case (kws.get(KEYWORD_NAME)):
System.out.println("Test");
break;
default:
System.out.println("Unknown");
}
}
}