What does the dollar sign java file names and the number of .class in it (name $ 1.class) mean?

When I compile my java Enum Day, it generates my 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 constant enumerations, but 8 overriding annotations, I realized that Dollar.class files are generated for each inner class or enumeration constant, but what about eight .class files, why is it created?

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:

  • Dayclass
  • Day $ 1.class
  • Day $ 2.class
  • Day $ 3.class
  • Day $ 4.class
  • Day $ 5.class
  • Day $ 6.class
  • Day $ 7.class
  • Day $ 8.class
+6
source share
2 answers

For each Enum literal, a class is generated that is named Enum plus an enumeration literal index, all of which are in addition to the class generated by Enum, the enum class with the suffix 8 is a SwitchMap map, which is a map that maintains a link for an enumerated index that is used by the switch if you release 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 
+1
source

I just copied and compiled your code, and the result on my machine is as follows:

  • Dayclass
  • 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 . Perhaps you had up to 8 inner classes, you compiled, and then you just deleted one ... Try opening .class files with a text editor, you could understand something else from readable characters ... For example, in my Day$1.class I can read the text "Today is Sunday."

0
source

Source: https://habr.com/ru/post/955022/


All Articles