Imagine what happens when you define nested classes or interfaces? For instance, if you define a two-level class, say Outer, and an inner class, say Inner, can Inner access the private instance variables of Outer? Here's some sample code:
public class Outer { private int outerInt = 20; public class Inner { int innerInt = outerInt; // Can Inner access outerInt? } }
Yes, it can. Since you define these classes within the same source code file, you might assume it to be obvious. However, it is not. The compiler generates separate bytecode files (.class) for the Outer and Inner classes. For the preceding example, the compiler creates two bytecode files: Outer.class and Outer$Inner.class. For your quick reference, the bytecode file of an inner class is preceded by the name of its outer class and a dollar sign.
To enable the...