Below is a snippet of code,
public enum Main {
INSTANCE;
private final static String STR = "abc";
private final Map<Integer, Character> map = new HashMap<>();
private final static int[] NUMS = { 1, 2, 3 };
private Main() {
for (int i = 0; i < STR.length(); i++)
map.put(NUMS[i], STR.charAt(i));
}
public char toChar(int i) {
return map.get(i);
}
public static void main(String[] args) {
System.out.println(Main.INSTANCE.toChar(2));
}
}
He released compiler errors below,
illegal reference to static field NUMS from initializer.
Why is it STRallowed staticbut NUMSnot?
source
share