What is an internal class
The class is defined inside other classes and is called an inner class.
Classification of inner classes
The inner class is divided into two types, the member inner class and the local inner class, respectively:
- Members Inner classes: and member variables and member methods defined in siblings
- Local inner classes: and local variables are defined at the sibling, including in the constructor method, member method body, and static method body
classOuter {classInnera {//member Inner class } { classInnerb {//local inner class } } Static { classINNERC {//local inner class } } PublicOuter () {classInnerd {//local inner class } } Public voidShow () {classInnerE {//member Inner class } }}
member internal class access characteristics
- An inner class can access all members of an external class directly.
- An external class must create an object to access the members of the inner class.
- Member inner classes are generally more private and static decorated
classOuter {Private intAge = 20; classInner { Public voidGetage () {System.out.println (age); } } Public voidmethod () {//symbol not found//getage ();Inner Inner=NewInner (); Inner.Getage();
}
}
Public class Test { publicstaticvoid main (string[] args) { new Outer (). New Inner (); Inner.getage (); }}
In the example above, you can directly pass new Outer (). New Inner (); To access the internal, this kind of access is generally unsafe, because the class is defined internally without being externally defined, there must be security requirements, which can be modified before the inner class multibyte private, if the member variable in the class, the external can only be accessed by the specified method, This time in the external method can provide validation, for example:
classOuter {Private classInnera { Public voidPlay () {System.out.println ("Play"); } } Public voidGetinner (String name) {if(Name.equals ("Lili")){ NewInnera (). Play (); } }} Public classTest { Public Static voidMain (string[] args) {Outer Outer=NewOuter (); Outer.getinner ("Lili"); }}
Of course, member inner classes also use static modifiers, which makes access easier, but static inner classes can only access static external member variables .
Public classTest { Public Static voidMain (string[] args) {Outer.Inner Inner=NewOuter.Inner (); Inner.getage (); }}classOuter {Private Static intAge = 20; Static classInner { Public voidGetage () {System.out.println (age); } } Public voidmethod () {Inner Inner =NewInner (); Inner.getage (); }}
Local internal class Access features
- All members of an external class can be accessed directly
- In a local location, you can create an inner class object to use the functionality provided by the local inner class
- Local internal class access local variables must be final decorated
classOuter {PrivateString name = "Lili"; Public voidShow () {intPhonenum = 10086; classInner {Final intAge = 20; Public voidGetage () {//phonenum =;//Error: (+) Java: The local variable referenced from the inner class must be the final variable or the actual final variable intMiniphonenum = Phonenum;//can accessSystem.out.println (age); System.out.println (phonenum);System.out.println (name); } } NewInner (). Getage (); }} Public classTest { Public Static voidMain (string[] args) {Outer Outer=NewOuter (); Outer.show (); }}
Why can local inner classes only access local variables of the final type?
A local variable is called as the method is called, and disappears as the call finishes. The contents of the heap memory do not disappear immediately. So, we add final retouching. When the final modifier is added, the variable becomes a constant. Since it is a constant. You're gone, I'm storing data 20 in memory, so I still have the data in use.
How to distinguish multi-layer variable with same name in inner class
classOuter { Public intnum = 10; classInner { Public intnum = 20; Public voidShow () {intnum = 30; SYSTEM.OUT.PRINTLN (num); System.out.println ( This. Num); //System.out.println (New Outer (). num);System.out.println (Outer. This. Num); } }}
Java Basics Java Inner class