Java Compiled Classes Contain Dollar Signs

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

Any risk using a single dollar sign `$` as a java class name?

It is bad style, and potentially risky to use $ in any identifier in Java. The reason it is risky is that the $ character is reserved for the use of the Java toolchain and third-party language tools.

  • It is used by Java compilers in "internal" class names for inner and nested classes.
  • It is used by Java compilers in the names of synthetic attributes.
  • It could be used by third-party code generators (e.g. annotation processors) for various purposes.
  • It could be used by other languages that target the JVM platform, and that might need to co-exist with your code.

You probably won't have technical issues with a plain $ classname at the moment (at least with respect to the standard Java toolchain). But there's always the possibility that this will change in the future:

  • They have (effectively) reserved the right to change this1.
  • There is a precedent for doing this in the _ example.

If you really, really need a one-character classname, it would be better to play it safe and use F or Z or something else that isn't reserved.

But to be honest, I think you'd be better off trying to implement (or just use) a real functional language than trying to shoe-horn a functional programming "system" into Java. Or maybe, just switch to Java 8 ahead of its official release. 'Cos I for one would refuse to read / maintain a Java codebase that looked like jquery.


I don't mean to create a functional lib for Java, just want to create a lib to maintain some common utilities I used. Again, I am a advocate of minimalism and feel suck with things like apache commons. The functional stuff is added to help me easier to manipulate collection(s).

If it is your code, you can do what you like. Make your own decisions. Act on your opinions. Be a "risk taker" ... :-). (Our advice on $, etcetera ... is moot.)

But if you are writing this code for a client or employer, or with the intention of creating a (viable) open source product, then you need to take account of other people's opinion. For example, your boss needs to have an informed opinion on how maintainable your code will be if you find a better paying job somewhere else. In general, will the next guy be able to figure it out, keep your code, fresh, etc ... or will it be consigned to the dustbin?


1 - JLS §3.8 states "The $ character should be used only in mechanically generated source code". That is saying "use it at your peril". The assumption is that folks who build their own source code generators can change them if the standard toolchain uses a bare $ ... but it is harder to change lots of hand written code, and that would be an impediment to upgrading.

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 class name containing dollar sign fails to compile if an inner class is present

You have a name conflict because you defined a top-level class A$B having the same name as the generated name for a static inner class B of class A. Since you have both, the compiler can't resolve the conflict.

The JLS says:

The $ character should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems.

Since you decided not to respect that rule, you got bitten by javac. I would just rename A$B to something else.

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.

What does dollar sign mean in generated variable names during debugging Java in InteliJ IDEA ? Is it a closure?

It's a reference to the anonymous inner class thats generated by this closure like construct. In general, inner classes are compiled and the class file name will be yourClassName$yourInnerClassName. In the case of an anonymous inner class declaration, since you don't name it explicitly, it will appear as a generated name using numbers. Perhaps this article will help?

Why does Scala place a dollar sign at the end of class names?

What you are seeing here is caused by the fact that scalac compiles every object to two JVM classes. The one with the $ at the end is actually the real singleton class implementing the actual logic, possibly inheriting from other classes and/or traits. The one without the $ is a class containing static forwarder methods. That's mosty for Java interop's sake I assume. And also because you actually need a way to create static methods in scala, because if you want to run a program on the JVM, you need a public static void main(String[] args) method as an entry point.

scala> :paste -raw
// Entering paste mode (ctrl-D to finish)

object Main { def main(args: Array[String]): Unit = ??? }

// Exiting paste mode, now interpreting.

scala> :javap -p -filter Main
Compiled from "<pastie>"
public final class Main {
public static void main(java.lang.String[]);
}

scala> :javap -p -filter Main$
Compiled from "<pastie>"
public final class Main$ {
public static Main$ MODULE$;
public static {};
public void main(java.lang.String[]);
private Main$();
}

I don't think there's anything you can do about this.

Why do I have so many CLASS files in bin folder?

Inner classes if any present in your class will be compiled and the class file will be ClassName$InnerClassName. In the case of Anonymous inner classes, it will appear as numbers.

Example:

public class TestInnerOuterClass {
class TestInnerChild{

}

Serializable annoymousTest = new Serializable() {
};
}

For the above code, the classes that will be generated are:

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

(ClassName)$.class files in class folder? (Netbeans)

They're the class files for anonymous classes. For example, if you write

  Foo = new Foo() {
....
}

then that's defining a class that extends class Foo but which has no name. The compiler will create a class file based on the name of the file that definition appears in, plus a dollar sign and a number.

There may be other reasons for "MainFile$N" classes as well, but this is one example.

They're created when you compile the Java source. They're needed to execute the program, but like any .class they will be regenerated on the next compilation of the file that defines them (in your case MainPro.java)



Related Topics



Leave a reply



Submit