/* Internal class Access features: 1, the inner class can access members in the outer class directly. 2, an external class must establish an inner class object to access the inner class. A design for the class. When analyzing a thing, it is found that there is something in the description of the thing, and that the thing is still accessing the content of the described thing. Then there are things that are defined as inner classes to describe. */class outer{private static int num = 31;class inner//inner class. {void Show () {System.out.println ("show Run ..." +num);} /*static void function ()//If a static member is defined in an inner class, the inner class must also be static. {System.out.println ("function run ..." +num); */}public void Method () {Inner in = new Inner (); In.show ();}} Class Innerclassdemo{public static void Main (string[] args) {//outer out = new Outer ();//out.method ();//Direct access to members in an inner class in an external class. Outer.Inner in = new Outer (). New Inner ();//in.show ();//If the inner class is static. Equivalent to an external class//outer.inner in = new Outer.Inner ();//in.show ();//If the inner class is static, the member is static. Outer.Inner.function ();}}
/* Why do internal classes have direct access to members of external classes? That's because the inner class holds references to external classes. external class name. this*/class outer{int num = 3;class inner{int num = 4;void Show () {int num = 5; System.out.println (Outer.this.num);}} void Method () {new Inner (). Show ();}} Class InnerClassDemo2 {public static void main (string[] args) {new Outer (). method ();}}
Java Internal classes