Well, I want to make this work
public static void main(String[] args) {
int cityId = 1234;
switch (cityId) {
case MOSCOW:
System.out.println("Moscow");
break;
case SOCHI:
break;
}
public enum Test {
MOSCOW,
NEWYORK,
SOCHI
}
So I want to link Test enum with city ids, is that possible? What is the best pattern for this situation?
Thank you!
You can’t mix that in a switch. Either you pass a
Testenum into the switch statement, or use the constant ids in the case statements.I’d suggest having a mapping
cityId <-> Test instanceand converting before calling the switch.Something like
Edit: note that you could put this mapping into the enum, add a cityId field to the enum and automatically fill the mapping from the array returned by
values(), much like Chris’ suggestion.That’s something we often do for similar things.