Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What means java filenames with a dollar sign and a number .class in it (name$1.class)? [duplicate]

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:

  • Day.class
  • Day$1.class
  • Day$2.class
  • Day$3.class
  • Day$4.class
  • Day$5.class
  • Day$6.class
  • Day$7.class
  • Day$8.class
like image 884
Eefret Avatar asked Oct 01 '13 13:10

Eefret


1 Answers

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
like image 176
Jans Avatar answered Sep 17 '22 13:09

Jans