Concepts / Nested Classes in Java

Nested Classes in Java

Compiling a class with a nested class creates two separate class files.

  • Programming

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?

  • One class file

  • Two class files

  • Three class files

Reveal answer
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.

WireFrameModel.javaone source definitionJava compilerWireFrameModel.classouter class fileWireFrameModel$Line.classnested class filecompiled byproducesproduces
When one source file defines a class with a nested class, what separate outputs does the compiler produce, and how do they relate to the single source definition?

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.

java
Notes on the code
  • WireFrameModel is the outer class and defines the source file WireFrameModel.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
sh
Output
WireFrameModel.class
WireFrameModel$Line.class
WireFrameModelouter class name$separatorLinenested class name.classclass file extension
What is the exact file name the compiler generates for the nested class, and how does it combine the outer and nested class names?

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

WireFrameModelouter class in sourceLinenested class in sourceWireFrameModel.classouter class fileWireFrameModel$Line.classnested class filecontainscompiles tocompiles to
How is the nested class contained within the outer class in the source code, and how does that containment differ from the separate class files produced on disk?

Mistakes When Looking for Compiled Classes

  • Common mistake

    Expecting a single class file for the whole source definition

    What it looks like

    Compiling WireFrameModel.java and opening only WireFrameModel.class, expecting to find Line inside it.

    Why it fails

    The source states that the nested class is stored in its own file, not inside the outer class's file.

    Do this instead

    Look 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 like

    Looking for Line.class in the build folder and concluding the nested class was not compiled.

    Why it fails

    The nested class file is named WireFrameModel$Line.class, combining the outer and nested class names.

    Do this instead

    Search 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

EASY

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.class and 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.