member Inner class (declared inside the class and outside of the method): 1 is a member of an external class: ① can have modifiers (4) ②static Final③ can invoke properties, methods of the outer class
2 Specific class features: ①abstract② can also define properties, methods, constructors in its internal
Local inner class (declared in the method of the Class):
Inner class function:
- For a better encapsulation, we know that the access modifiers of the normal class (non-intrinsic Class) cannot be private or protected, while the inner class can. When we declare an inner class as private, only the outer class can access the inner class, which hides the inner class well.
- An inner class can inherit (extends) or implement (implements) other classes or interfaces without being affected by an external class
- The inner class can directly access the fields and methods of the outer class, even if it is decorated with private, instead, the outer class cannot directly access the members of the Inner class
Static inner class: As a static member of a class, exists inside a class
A static inner class is a member of an external class, but an object of a static inner class can be created directly without creating an object of the outer class.
Static inner classes can reference static member variables and static methods of external classes, but cannot refer to ordinary members of external classes
PackageCom.yyx.pratice; Public classTestinner {PrivateString outername = "outer"; Private Static intID = 123; PrivateInner Inner =NewInner (); Public Static classInner { Public voidprint1 () {System.out.println (ID); //System.out.println (outername); } Public Static voidPrint2 () {System.out.println (ID); } } Public voidShow () {inner.print1 (); } Public Static voidMain (string[] args) {Testinner Testinner=NewTestinner (); Testinner.show (); TestInner.Inner.print2 (); }}
Second, Member inner class: As a member of a class, exists in the interior of a class
A member inner class can invoke all members of an external class, but only after an object of the outer class has been created can the external member be called
PackageCom.yyx.pratice; Public classTestinner {PrivateString outername = "outer"; //external classes cannot directly access members of inner classes, they need to instantiate inner class objects PrivateInner Inner =NewInner (); Public classInner {PrivateString innername = "inner"; Public voidShow () {System.out.println (outername);//members of external classes can be accessed directly } } Public voidShow () {System.out.println (inner.innername); Inner.show (); } Public Static voidMain (string[] args) {Testinner outer=NewTestinner (); Outer.show (); //instantiate an inner classTestinner.inner Inner = outer.NewInner (); Inner.show (); }}
Third, local inner class: exists in the interior of a method
The local inner class can only be used inside the method, and once the method executes, the local inner class is removed from memory.
It is important to note that if a local variable in his method is to be used in a local inner class, then the local variable needs to be defined as the final
PackageCom.yyx.pratice; Public classInnerclass { Public Static voidMain (string[] args) {Student stu=NewStudent (); Stu.getperson (). Eatrice (); }}InterfacePerson { Public voideatrice ();}classStudent { PublicPerson Getperson () {/** Anonymous Inner class*/ return NewPerson () {@Override Public voidEatrice () {System.out.println ("I'll eat rice!" "); } }; }}
Java Internal class