Java compiled classes contain dollar signs

I am using Eclipse as my development IDE. I also use it to export my application to a .jar file. When I look at my classes in a .jar file, some of my classes contain the name of that class, the dollar sign, and then the number. Example:

  • Find $ 1.class
  • Find $ 2.class
  • Find $ 3.class
  • Find.class

I noticed that this is done in large classes. Is it because classes get so big that compiles it into multiple classes? I googled and looked at several forums, and searched for Java documentation, but did not find anything even related to it. Can someone explain?

+48
java class compilation
Jul 09 2018-12-12T00:
source share
4 answers

Inner classes, if present in your class, will be compiled, and the class file will be ClassName$InnerClassName . In the case of anonymous inner classes, it will be displayed as numbers. The class size (Java code) does not generate multiple classes.

eg. given this part of the code:

 public class TestInnerOuterClass { class TestInnerChild{ } Serializable annoymousTest = new Serializable() { }; } 

The classes to be generated will be:

  • TestInnerOuterClass.class
  • TestInnerOuterClass$TestInnerChild.class
  • TestInnerOuterCasss$1.class

Update:

Using an anonymous class is not considered bad practice, it just depends on the use.

Check out this SO discussion

+63
Jul 09 '12 at 3:45
source share
— -

This is because you have anonymous classes in this larger class. They are compiled using this naming convention.

See Anonymous Class Puzzle

+10
Jul 09 '12 at 3:45
source share

In addition to the above cases provided by @mprabhat, other cases may be:

  • If the class contains the enum variable, a separate class will also be created for this. The name of the generated .class will be ClassName $ Name_of_enum .
  • If your class X inherits, that is, extends another class Y , then there will be a class .class generated with the name ClassName $ 1.class or ClassName $ 1 $ 1.class
  • If your class X implements the Y interface, then a .class class will be created with the name ClassName $ 1.class or ClassName $ 1 $ 1.class .

These cases are the output of my check for .class files in jar.

+5
Aug 28 '15 at 15:29
source share

To answer your comment, anonymous classes are bad. They are most absent. Consider this to designate a JButton action listener:

 JButton button = new JButton(...); button.addActionListener(new ActionListener() { ... }); 

or do it case insensitive sorting by property "name"

 Collections.sort( array, new Comparator<Foo>() { public int compare(Foo f1, Foo f2) { return f1.getName().toLowerCase().compareTo(f2.getName().toLowerCase()); } }); 

You will also see many Runnable and Callable executed as anonymous classes.

0
Jul 11 '12 at 1:16
source share



All Articles