I have a program below:
package com.company;
enum Color {
RED, GREEN;
Color() {
System.out.println(Main.getRegionName(this));
}
}
public class Main {
public static String getRegionName(Color region) {
switch (region) {
case RED:
return "red";
case GREEN:
return "green";
default:
return "false";
}
}
public static void main(String[] args) {
Main m = new Main();
Color color = Color.RED;
}
}
When I run the program, I got the exceptions below:
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.company.Main.getRegionName(Main.java:13)
at com.company.Color.<init>(Main.java:7)
at com.company.Color.<clinit>(Main.java:4)
at com.company.Main.main(Main.java:25)
Caused by: java.lang.NullPointerException
at com.company.Color.values(Main.java:3)
at com.company.Main$1.<clinit>(Main.java:13)
... 4 more
What's the reason for it? Is the 'this' initialized for the Color class when it calls Main.getRegionName(this) in its constructor?