The inner class, by definition, is the class defined in the class, and here's a quick introduction:
Access rules for internal classes:
1, the inner class can access the members of the external class directly, including the private.
Members in an external class can be accessed directly because the inner class holds a reference to an external class, in the form of an external class name. This
2, the outer class must establish an inner class object to access the inner class.
class Outer{private int x = 3;class inner//inner class {//int x = 4;void function () {//int x = 6; System.out.println ("Innner:" +outer.this.x);}} /**/void method () {Inner in = new Inner (); In.function ();}} Class innerclassdemo{public static void Main (string[] args) {Outer out = new Outer (); Out.method ();//Direct access to members within the inner class. Outer.Inner in = new Outer (). New Inner ();//in.function ();}}
Access format:
1, when the inner class is defined on the member position of the outer class, and is not private, it can be in an external other class.
You can create an inner class object directly.
Format
The outer class name. Internal class Name Variable name = External Class object. Inner class object;
Outer.Inner in = new Outer (). New Inner ();
2, when the inner class is in the member position, it can be decorated by the member modifier.
For example, private: Encapsulates inner classes in the outer class.
Static: The inner class has static properties.
When an inner class is modified by static, only static members in the outer class can be accessed directly. Access restrictions have occurred.
In other external classes, how do you directly access the non-static members of the static inner class?
New Outer.Inner (). function ();
In other external classes, how do you directly access static members of static internal classes?
Uter. Inner.function ();
Note: When a static member is defined in an inner class, the inner class must be static.
When a static method in an external class accesses an inner class, the inner class must also be static.
When describing things, there is something inside the thing, which is described by an inner class.
Because internal transactions are using the contents of external things.
The class outer{private static int x = 3;static class inner//static inner class "{static void function () {System.out.println (" Innner: "+ x);}} Static Class Inner2{void Show () {System.out.println ("Inner2 Show");}} public static void Method () {//inner.function (); new Inner2 (). Show ();}} Class innerclassdemo2{public static void Main (string[] args) {outer.method ();//outer.inner.function ();//new Outer.Inner (). function ();//Direct access to members within the inner class. Outer.Inner in = new Outer (). New Inner ();//in.function ();}}
The inner class is defined locally, which is defined in the member function of the class.
1, can not be modified by member modifiers
2, you can access members in an external class directly, because you also hold references in external classes.
However, you cannot access the variables in the local area where it resides. Only the final decorated local variables can be accessed.
Class Outer{int X = 3;void method (final int a) {final int y = 4;class inner{void function () {System.out.println (y);}} New Inner (). function ();}} Class innerclassdemo3{public static void Main (string[] args) {Outer out = new Outer (); Out.method (7); Out.method (8);}}
Anonymous inner class:
1, the anonymous inner class is actually the shorthand format for the inner class.
2, the premise of defining an anonymous inner class:
The inner class must inherit a class or implement an interface.
3, Anonymous inner class format: New parent class or interface () {Defines the contents of a subclass}
4, in fact, the anonymous inner class is an anonymous subclass object. And the object is a little fat. Can be understood as an object with content.
5, it is best not to have more than 3 methods defined in an anonymous inner class.
Abstract class absdemo{abstract void Show ();} Class Outer{int x = 3;/*class Inner extends absdemo{int num = 90;void Show () {System.out.println ("show:" +num);} VOID abc () {System.out.println ("hehe");}} */public void function () {//absdemo a = new Inner ();//inner in = new Inner ();//in.show ();//in.abc (); Absdemo d = new Absdemo () {int num = 9;void Show () {System.out.println ("num===" +num);} VOID abc () {System.out.println ("haha");}}; D.show ();//d.abc ();//compilation Failed;}} Class InnerClassDemo4 {public static void main (string[] args) {new Outer (). function ();}}
15. Introduction to internal classes in Java