Sub topic 13
How Nested Classes Compile: Outer$Inner.class
Assume you can write and compile ordinary Java classes, understand static and instance members, and know interfaces, abstract classes, and access modifiers. This chapter focuses on what the Java compiler creates for nested classes.
Source nesting versus compiled classes
A nested class is written inside another class, but the Java Virtual Machine loads classes as separate class files. The compiler usually converts each nested declaration into its own class file. The dollar sign in the file name records the nesting relationship.
| Java declaration | Typical class file |
|---|---|
| `Outer` | `Outer.class` |
| `Outer.Inner` | `Outer$Inner.class` |
| `Outer.Local` declared in method `run` | `Outer$1Local.class` |
| Anonymous class in `run` | `Outer$1.class` |
The exact numbering depends on the order of local and anonymous classes in the source file. These names are compiler output details, not names you normally write in Java source.
public class Outer {
static class StaticNested {
void print() {
System.out.println("static nested");
}
}
class Inner {
void print() {
System.out.println("inner");
}
}
void createClasses() {
class Local {
void print() {
System.out.println("local");
}
}
Runnable anonymous = new Runnable() {
@Override
public void run() {
System.out.println("anonymous");
}
};
new Local().print();
anonymous.run();
}
public static void main(String[] args) {
Outer outer = new Outer();
new StaticNested().print();
outer.new Inner().print();
outer.createClasses();
}
}Compile the source with the Java compiler:
javac Outer.javaThe output directory contains files similar to these:
Outer.class
Outer$StaticNested.class
Outer$Inner.class
Outer$1Local.class
Outer$1.classWhat the compiler adds
A non-static inner class needs access to a particular outer object. The compiler therefore represents that relationship with hidden implementation details. Conceptually, an inner object contains a reference to its enclosing Outer object. The source expression outer.new Inner() supplies that reference.
A local or anonymous class may also receive hidden fields for captured local variables. If a class uses a method parameter called name, the generated object must store that value because the method may finish before the object is used.
The generated class can also contain compiler-created constructors, fields, or methods. Their exact form can vary between Java versions and compiler implementations, so do not depend on those details in application code.
Private access and nestmates
Nested classes can use private members of their enclosing class, and the enclosing class can use private members of a nested class. Java treats them as belonging to the same nest.
Since Java 11, the class-file format has explicit nestmate metadata for this relationship. This lets the JVM handle permitted private access directly. Older Java compilers sometimes generated synthetic bridge methods to perform the access.
The source-level rule is stable: private access between an enclosing class and its nested classes is allowed. The class files may differ depending on the compiler and target Java version.
> callout note > Do not parse or depend on Outer$Inner.class names, synthetic fields, or hidden constructor parameters. They describe one compilation result and can change when the source, compiler, or Java target changes.
Practical consequences
The separate class files matter when packaging, inspecting, or debugging an application.
- Copy every generated class file when distributing compiled code.
- Keep nested class files together with their enclosing class.
- Stack traces may show names such as
Outer$Inner. - Reflection reports binary names with dollar signs.
- A retained inner object can retain its outer object through the hidden reference.
A static nested class does not need an outer instance. An inner, local, or anonymous class may need one, depending on where it is declared and what it captures.
Exercise
After compiling Outer.java, which class file normally represents the non-static member class Inner, and what hidden relationship does it usually represent?
Show answer
The file is Outer$Inner.class. Its generated representation usually includes a reference to the particular Outer instance that created it.