In response to the risky question @Jon Skeet:
Another class then refers to Bar, so the JVM needs code for it ... how would you suggest finding a file?
Suppose (hypothetically) that the format of a Java class is nested / inner classes, inserting them into a class file for the outermost class. The binary name for the string is " Lsome/pkg/Foo$Bar; ". The class loader can split the name into the $ character, use the first part to find the class file for Foo, and then go to the built-in representation of the Bar class.
I think the real reason that inner / nested classes have separate class files is historical. IIRC, Java 1.0 did not support nested or inner classes, so the corresponding class file formats did not need them. When Java 1.1 was created (supporting inner / nested classes), Sun wanted the class file format to be compatible with the class files created by the Java 1.0 compiler. Therefore, they decided to implement inner / nested classes as separate class files, using the reserved character " $ " in the binary class name.
A second possible reason is that the flat format makes class loading easier than the hypothetical inline format.
And finally, there was (and still is) no convincing reason for them NOT to use the flat file format. Perhaps this creates some minor scratches when some programmer wants to load inner classes using Class.forName() , but this is a pretty rare occurrence ... and the solution is straightforward.
Stephen c
source share