when to use:
When the content in Class A is accessed directly by Class B, and Class A also needs to create an object of B, and Access B's content, B is called an inner class (built-in class, nested Class).
Access method:
An inner class can directly access all members of an external class, contain private, and an external class must create an object of the inner class to access the members of the inner class.
Class outer//external class {private int num = 4; Public CALSS inner//inner class {void show () {System.out.println ("num=num"); }} void Method () {/*outer.*/inner in = new/*outer.*/inner (); In.show (); }}class innerclassdemo{public static void Main (string[] args) {Outer out = n EW Outer (): Out.method (); }}public static void Main (string[] args) {Outer out = new Outer (); Out.method (); Test Case one: direct access to the non-static members of the inner inner class in outer//create an inner class object, the inner class as a member, there should be an outer class object, and then an inner class object. Outer.Inner in = new Outer (). New Inner (); In.show (); Test case two: calls to non-static members in the static inner class//Because the inner class is static, so there is no need to create an outer object, just create the inner class object directly. Outer.inner2 in = new Outer (). New Inner2 (); In.show2 (); } How to access:
why does an inner class have direct access to members in an external class?
Because the inner class actually holds a reference to the outer class, the outer class. This, for static inner classes, does not hold external classes. This is a direct use of the external class name
local inner class features:
An anonymous inner class is, in fact, a subclass object with content.
Class innerclassdemo4{public static void Main (string[] args) { Outer out = new Outer (); Absdemo a = Out.getobject ();//polymorphic a.show (); <span style= "White-space:pre" ></span> }}
Now you do not want to create a specific subtype, but also want to create a subclass of Absdemo object, how to implement?
It is possible to use the parent class directly, but in this case it is an abstract class, how is the new object? → Override abstract methods.
New Absdemo () { //overrides the abstract Show Method void Show () { System.out.println ("num=" +num); <span style= "White-space: Pre "></span> }}
This is the legend of a Absdemo subclass object, but this is a sub-class with content is not to be.
Format: New parent class or interface () {Content of child class}
In fact, the anonymous inner class is the simplified form of the inner class; Note that the anonymous inner class has premises, and must inherit the parent class or implement the interface.
Note:
If the inner class has static members, how to access:
Since static inner classes are already loaded with external classes, and static members are loaded with the load of the class, no objects are required and can be called directly with the class name.
Outer.Inner2.staticShow ();
Static member is not allowed to be defined in the static void Show1 () {}//Non-static inner class; Static constants are allowed only in non-static inner classes (static final)
If you want to define a static member in an inner class, you must also have the inner class be statically decorated.
Java Internal classes