Compiling

The Java compiler compiles inner classes as normal classes, changing their names to ensure uniqueness and indicate scope, and usually adding some code to them to make them work as described.

If you have a .java file with top-level class and an inner class, it will be compiled into two classes.

Outer.java

public class Outer {
  class Inner {

  }
}

OtherOuter.java

public class OtherOuter {
  class Inner {

  }
}

This is compiled to the files:

Outer.class
Outer$Inner.class

OtherOuter.class
OtherOuter$Inner.class
* This is why we can have inner classes with the same names.

We don't use the name Outer$Inner in our code.


Prev -- Up -- Next