Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
524 views
in Technique[技术] by (71.8m points)

java - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...