What Is the $1 in Class File Names

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

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

java compiled classes contain dollar signs

Inner classes, if any present in your class, will be compiled and the class file will be ClassName$InnerClassName. In case of Anonymous inner classes, it will appear as numbers. Size of the Class (Java Code) doesn't lead to generation of multiple classes.

E.g. given this piece of code:

public class TestInnerOuterClass {
class TestInnerChild{

}

Serializable annoymousTest = new Serializable() {
};
}

Classes which will be generated will be:

  1. TestInnerOuterClass.class
  2. TestInnerOuterClass$TestInnerChild.class
  3. TestInnerOuterCasss$1.class

Update:

Using anonymous class is not considered a bad practice ,it just depends on the usage.

Check this discussion on SO

Java Class Files filename$1.class... etc Question

These classes are created from anonymous classes created in your applet - most likely event listeners and such.

They would be created (maybe with other names) by other compilers, too.

If you really want to avoid them, program without anonymous (and other inner) classes. But this results often in an ugly style, so this is not recommended.

If you don't want to upload all the individual class files to the server (and then the browsers having to fetch them all individually), think about putting them all in one jar file, and referencing this in your applet-tag.

why is filename$1.class generated?

Using javap on classes in the package de.lhorn:

Day.class

public final class de.lhorn.so.Day extends java.lang.Enum<de.lhorn.so.Day> {
public static final de.lhorn.so.Day SUNDAY;
public static final de.lhorn.so.Day MONDAY;
public static final de.lhorn.so.Day TUESDAY;
public static final de.lhorn.so.Day WEDNESDAY;
public static final de.lhorn.so.Day THURSDAY;
public static final de.lhorn.so.Day FRIDAY;
public static final de.lhorn.so.Day SATURDAY;
public static de.lhorn.so.Day[] values();
public static de.lhorn.so.Day valueOf(java.lang.String);
static {};
}

Enum1.class

public class de.lhorn.so.Enum1 {
public de.lhorn.so.Enum1();
public static void main(java.lang.String[]);
}

Enum$1.class

class de.lhorn.so.Enum1$1 {
static final int[] $SwitchMap$de$lhorn$so$Day;
static {};
}

So the switch causes javac to generate the additional static final int[] $SwitchMap$de$lhorn$so$Day;.

what is the signficance of the Java class files of the sort myClass$1.class?

These are anonymous inner classes in bytecode form. The compiler gives them numerical names starting with 1 (it is not allowed in Java to have a class name starting with a number, but it is possible in the bytecode, so the compiler does it to avoid name clashes, I guess). Normal (named) inner classes are named like OuterType$InnerType.class.

What does dollar followed by an index mean in a class name?

com.x.y.ClassName$5 means "the fifth anonymous inner class in com.x.y.ClassName"

Why does classname$1.class generate in this situation?

Class TestHolder needs to call the private constructor in Test. But it's private, and can't actually be called from another class. So the compiler plays a trick. It adds a new non-private constructor to Test which only it knows about! That constructor takes an (unused) instance of this anonymous class Test$1 -- which nobody knows exists. Then TestHolder creates an instance of Test$1 and calls that constructor, which is accessible (it's default-protected.)

You can use javap -c Test (and javap -c Test\$1, and javap -c Test\$TestHolder) to see the code. It's quite clever, actually!

Reflection Class.forName() finds classes classname$1 and classname$2, what are they?

Those are anonymous inner classes. For example:

public class Foo {
public static void bar() {
Runnable runnable = new Runnable() {
@Override public void run() {}
};
}
}

This will create a class Foo$1 which implements Runnable.

The naming of Java Class file

Those are the anonymous inner classes defined inside the main class.



Related Topics



Leave a reply



Submit