Java Reference
In-Depth Information
// A local class
class AnotherLocalInnerClass {
}
// Anonymous Inner class
new Object() {
};
}
public void testMethod2() {
// A local class. Its name is the same as a local class in testMethod1() method
class AnotherLocalInnerClass {
}
// Another local class
class TestLocalClass {
}
}
}
Inner Classes and the Compiler Magic
Inner classes are implemented with the help of the compiler. The compiler does all the magic behind the scenes for
the features provided by inner classes. It alters your code and adds new code to implement inner classes. Here is the
simplest example of an inner class:
public class Outer {
public class Inner {
}
}
When the
Outer
class is compiled, two class files are generated:
Outer.class
and
Outer$Inner.class
. If you
decompile these two class files, you get the following output. You can use any available decompilers for class files.
Some Java class file decompilers are available free on the Internet. You can also use the
javap
tool, which ships
with the JDK, to decompile class files. The
javap
utility is located on your machine in
JAVA_HOME\bin
folder, where
JAVA_HOME
is the JDK installation folder.
// Decompiled code from Outer.class file
public class Outer {
public Outer() {
}
}
// Decompiled code from Outer$Inner.class file
public class Outer$Inner {
final Outer this$0;
public Outer$Inner(Outer outer) {
this$0 = outer;
super();
}
}