What means java filenames with a dollar sign and a

2019-06-17 05:49发布

问题:

This question already has an answer here:

  • java compiled classes contain dollar signs 4 answers

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

回答1:

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


回答2:

I have just copied and compiled your code and the result on my machine is the following:

  • Day.class
  • Day$1.class
  • Day$2.class
  • Day$3.class
  • Day$4.class
  • Day$5.class
  • Day$6.class
  • Day$7.class

No Day$8.class has been created. Maybe you had before 8 inner classes, you compiled and then you just removed one ... Try to open with a text editor the .class files, you could understand something more from the readable characters ... For example in my Day$1.class I can read the text "Today is Sunday".