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.
7 Answers
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)
- 40,547
- 20
- 99
- 126
-
Terminology: not only "inner classes" but all "nested classes" are compiled to such files – user85421 Jun 25 '09 at 20:55
-
Don't forget about anonymous classes. Technically they are already covered by "inner classes" but they don't always look like classes so people maybe surprised to see them appear. Also, the new Java 8 lambdas look like they will be generating additional classes when compiled. – Sled Nov 19 '11 at 16:00
-
@SimonNickerson Is there any way avoid such external class file generation. I mean is there a way to force the compiler to produce all the classes in the same `.class` file. – Ran Aug 07 '18 at 04:38
-
That means, will the Foo$1.class file be generated? even if we haven't used the "implements Helper" = Helper interface – sudhee_bsp Sep 26 '20 at 06:09
You get more .class fils from a single source file if
the class contains inner classes or static inner classes. Inner classes can nest. Their names are
<outer class name>$<inner class name>.inner interfaces which are always static.
anonymous inner classes (which in fact are plain inner classes without a name)
package access interfaces and classes before and after your main class. You can have an arbitrary number of package access classes and interfaces in a single Java source file. Usually small helper objects that are only used by the class are just put into the same file.
- 9,058
- 8
- 50
- 79
One java source file can generate multiple class files, if your class contains inner classes. Anonymous inner classes are represented by your numbered class files.
- 390,936
- 96
- 800
- 764
Every class in java belongs to a .java-file, but a .java-file can contain multiple classes. That includes inner and anonymous classes. The .class-files generated for inner classes contain a '$' in their name. Anonymous inner classes get numbers.
- 48,735
- 46
- 144
- 201
To add to the answers above, this is another good example of generated inner classes based on Comparators (each Comparator is compiled in a different MyClass$X.class):
public class MyClass {
...
public void doSomething() {
...
Collections.sort(list, new Comparator<MyObj>() {
public int compare(MyObj o1, MyObj o2) {
...
}
});
...
}
...
}
- 1,715
- 18
- 15
If there is one X.java file and if it contains 4 collections.sort() {} then after compilation X.class,X$1.class,X$2.class,X$3.class,X$4.class will get generated.
In case of inner class and static inner class more .class files get generated.
- 59
- 2
- 15
More than one class will be generated on compilation, Only if your class is having inner class.
refer: Why does Java code with an inner class generates a third SomeClass$1.class file?
- 1
- 1
- 34,491
- 66
- 168
- 213
-
1Well, a source file can also contain separate, non-nested classes (if they're not public). – Michael Borgwardt Jun 26 '09 at 07:45
-
1not ONLY if it is having inner classes (there are also anonymous classes,nested classes, ...). – user85421 Jun 26 '09 at 09:01