Can someone explain how the below code works without any exceptions. I am thinking when new instance is created for SUNDAY it creates a new instance for MONDAY (inside SUNDAY) too and then SUNDAY again (inside MONDAY) and so on... Something like recursive as they both are part of the class Week. But my understanding is wrong because the below code is working fine.
public class Week {
public static final Week SUNDAY = new Week("SUNDAY",0);
public static final Week MONDAY = new Week("MONDAY",1);
private String name;
private int val;
private Week(String name, int val) {
this.name = name;
this.val = val;
}
public int getIndex() {
return this.val;
}
@Override
public String toString() {
return this.name;
}
}
I got this doubt when I was reading about java enums.