While the other answers contain helpful suggestions, I’d like to explain the reason for your error and the limitations of Java enums.
The reason for your syntax error is that in Java (and most other programming languages) an enum constant is an identifier and an identifier has to start with a letter (or underscore, certainly not recommended). 0 is not a letter.
This in turn also means that Country.valueOf("0") is never going to work no matter what you do (one might have imagined overriding the method in your enum, but that is not possible).
Two suggestions:
Use another name, one that starts with a letter.
NULL_BLANK (""); //for null blank
Use a null to represent no country.
For a different name I’m thinking COUNTRY_0, BLANK or ZERO. No matter if you use item 1. or 2. above you will have to write your own lookup method as in Joakim Danielson’s answer, for example.
Edit: A possibly nicer solution may be an enum where the constructor accpets both key and country name as arguments:
public static enum Country {
MALAYSIA ("MY", "Malaysia"),
SINGAPORE ("SG", "Singapore"),
INDONESIA ("ID", "Indonesia"),
NULL_BLANK ("0", ""); //for null blank
private static final Map<String, Country> BY_KEY
= Arrays.stream(values()).collect(Collectors.toMap(c -> c.key, c -> c));
public static Country of(String key) {
return BY_KEY.get(key);
}
private final String key;
private final String country;
private Country(String key, String name) {
this.key = key;
this.country = name;
}
public String getCountry() {
return this.country;
}
}
One advantage is it allows for nicer enum constant names, MALAYSIA instead of MY, etc. I have provided the of method for lookup. Use like for example:
System.out.println("MY -> \"" + Country.of("MY").getCountry() + "\"");
System.out.println("0 -> \"" + Country.of("0").getCountry() + "\"");
Output:
MY -> "Malaysia"
0 -> ""