Nested Classes in Java
Compiling a class with a nested class creates two separate class files.
One Source Definition, How Many Outputs
Suppose you write one class and place another class inside it. The nested class sits physically inside the outer class's source code, so it is tempting to assume the compiled result is a single file, with the inner class tucked inside the outer class's output. Before reading on, make a guess. The answer determines where you will look for compiled classes later, whether you are debugging a project or inspecting a build folder.
What do you think happens?
You compile one source file that defines an outer class with one nested class inside it. How many class files does the compiler produce?
Reveal answer
Two class files
The source states that compiling a class with a nested class creates two separate class files, and that the expectation is one class file per class. The outer class and the nested class each get their own file.
Tracing What the Compiler Emits
Follow the path of one source definition through the compiler. A single source file goes in. Nothing in the code asks for two files; the compiler handles the split automatically. Two class files come out: one for the outer class and one for the nested class. The nested class is stored in its own file, not inside the outer class's file.
The diagram shows the flow in words. One source definition enters the compiler, and two class files leave it. The outer class's own file is one of the two outputs. The nested class's file is the other, and its name ties it back to the class that contains it.
Reading the Nested Class File Name
In the source example, the nested class's file is named WireFrameModel$Line.class. The name combines the outer class name, a dollar sign, and the nested class name. The dollar sign acts as the link between the two names, so the file name itself records which class contains the nested class.
WireFrameModelis the outer class and defines the source fileWireFrameModel.java- Line is the nested class, written inside the outer class's braces
- Nothing in this code asks for a second file; the compiler produces one per class
WireFrameModel.class
WireFrameModel$Line.classContainment in Source Versus Files on Disk
There are two different views of the same design. In the source code, Line is contained within WireFrameModel: it is written between the outer class's braces. On disk, that containment disappears. Each class has its own class file, and the nested class's file sits alongside the outer class's file rather than inside it.
Mistakes When Looking for Compiled Classes
Common mistake Expecting a single class file for the whole source definition
What it looks likeCompiling WireFrameModel.java and opening only WireFrameModel.class, expecting to find Line inside it.
Why it failsThe source states that the nested class is stored in its own file, not inside the outer class's file.
Do this insteadLook for a second file named with the $ pattern, such as WireFrameModel$Line.class.
Common mistake Searching for a file named Line.class
What it looks likeLooking for Line.class in the build folder and concluding the nested class was not compiled.
Why it failsThe nested class file is named
WireFrameModel$Line.class, combining the outer and nested class names.Do this insteadSearch for the outer class name followed by a dollar sign and the nested class name.
Common mistake Assuming the dollar sign is a typo or a sign of an error rather than the naming link between the two classes
Predicting the Output of Your Own Definition
A single file, Garage.java, defines a class named Garage that contains one nested class named Door. Before compiling, predict the names of the class files the compiler produces, then check your prediction by running javac on the file and listing the .class files in the folder.
Hints
- Start with one class file per class: how many classes are there in total?
- Use the naming pattern from the source: outer class name, a dollar sign, then the nested class name.
- Expected result:
Garage.classand Garage$Door.class.
Key Takeaways
- Compiling a class with a nested class creates two separate class files, one for the outer class and one for the nested class.
- The nested class is stored in its own file, not inside the outer class's file.
- The nested class file is named by combining the outer and nested class names with a dollar sign, as in
WireFrameModel$Line.class. - Count class files by class, not by source file: a single source definition with a nested class yields two class files.