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

I have a Java project running in Eclipse with a main executable file called GreatPlaces.java . In my /bin I would assume that I have only one CLASS file named GreatPlaces.class . However, I have a couple of them, besides GreatPlaces.class , I also have GreatPlaces$1.class , GreatPlaces$2.class ... GreatPlaces$22.class . Can someone explain this to me? Thanks.

+4
source share
3 answers

Inner classes, if any of those present in your class is compiled and the class file is ClassName$InnerClassName . The inclusion of anonymous inner classes will appear as numbers.

Example:

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

Generated classes:

  • TestInnerOuterClass.class
  • TestInnerOuterClass $ TestInnerChild.class
  • TestInnerOuterCasss $ 1.class
+4
source

The dollar sign is used by the compiler for inner classes.

Sign

$ represents inner classes. If it has number after $ , then it is an anonymous inner class. If it has name after $ , then this is only an inner class.

So, in your cases, they represent the inner classes of annonymouse

+4
source

These class files correspond to the anonymous inner classes that you use in the program.

Here is an example of an event handler that will be compiled into its own .class file:

 button.addActionLister(new ActionListener() { public void actionPerformed(ActionEvent e) { .... } }); 
+2
source

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