Search code examples
javaclassjavac

Multiple .class files generated for a class?


Out of curiosity, why are sometimes multiple Java .class files generated for a class after compilation? For example, my application has six classes. For one class, a total of 10 .class files has been generated, starting from MyClass#1 up to MyClass#10.


Solution

  • These are for inner classes and static nested classes. The ones with numbers are anonymous inner classes.

    For example:

    
    class Foo {
       class Bar { }
       static class Baz { }
       void run() {
          Helper t = new Helper() {
             int helpMethod() {
                return 2;
             }
          };
        }
    }
    

    This will produce class files Foo.class, Foo$Bar.class, Foo$Baz.class and Foo$1.class (for the implementation of the Helper interface)