When i compile my java Enum Day, it generates his Day.class file and 8 Day$#.class Files, so i want to know why the compiler generates 8 $#.class instead of 7, because i have 7 enum constants, but 8 override annotations, i have understood that the Dollar.class files are generated for every inner class or by enum constants, but what about the eight .class file what it is generated for?
package com.kaissersoft.test.objective.one.three.enums;
public enum Day{
SUNDAY(01){
@Override
public void salute(){
System.out.println("Today is Sunday");
}
},
MONDAY(02){
@Override
public void salute(){
System.out.println("Today is Monday");
}
},
TUESDAY(03){
@Override
public void salute(){
System.out.println("Today is Tuesday");
}
},
WEDNESDAY(04){
@Override
public void salute(){
System.out.println("Today is Wednesday");
}
},
THURSDAY(05){
@Override
public void salute(){
System.out.println("Today is Thursday");
}
},
FRIDAY(06){
@Override
public void salute(){
System.out.println("Today is Friday");
}
},
SATURDAY(07){
@Override
public void salute(){
System.out.println("Today is Saturday");
}
};
int dayNumber;
Day(final int day){
dayNumber = day;
}
public int getDayNumber(){
return dayNumber;
}
@Override
public String toString(){
switch(this){
case SUNDAY:
System.out.println("Sunday("+this.getDayNumber()+")");
break;
case MONDAY:
System.out.println("Monday("+this.getDayNumber()+")");
break;
case TUESDAY:
System.out.println("Tuesday"+this.getDayNumber()+")");
break;
case WEDNESDAY:
System.out.println("Wednesday("+this.getDayNumber()+")");
break;
case THURSDAY:
System.out.println("Thursday("+this.getDayNumber()+")");
break;
case FRIDAY:
System.out.println("Friday("+this.getDayNumber()+")");
break;
case SATURDAY:
System.out.println("Saturday("+this.getDayNumber()+")");
break;
}
return super.toString();
}
//Abstract method to the day Salute
public abstract void salute();
}
And it generates this:
By each Enum literal is generated a class identified by the name of the Enum plus the index of the enum literal, all this apart of the Enum generated class, the enum class with the suffix 8 is a map called SwitchMap wich is a map that maintaing a reference of enum literal index that are used by the switch, if you excute this code you can see it.
try{
Class< ? > c = Class.forName( "com.kaissersoft.test.objective.one.three.enums.Days$8" );
Field[] fs= c.getDeclaredFields();
for( Field f: fs ){
System.out.println( f.toString() );
}
}catch( ClassNotFoundException cne){
cne.printStackTrace();
}
// Output
static final int[] com.kaissersoft.test.objective.one.three.enums.Days$8.$Switch
Map$com$kaissersoft$test$objective$one$three$enums$Days
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With