1. Local inner class
1 /*2 local inner class3 A: Members of external classes can be accessed directly4 B: In a local location, you can create an inner class object that invokes an inner class method through an object to use the local inner class functionality5 6 Interview questions:7 local internal class access to local variables considerations?8 A: Local internal class access local variables must be final decorated 9 B: Why?Ten a local variable is called as the method is called, and disappears as the call finishes. One the contents of the heap memory do not disappear immediately. So, we add final retouching. A when the final modifier is added, the variable becomes a constant. Since it is a constant. You are gone. - I'm storing data 20 in memory, so I still have the data in use. - */ the classOuter { - Private intnum = 10; - - Public voidmethod () { + //int num2 =; - //final int num2 =; + classInner {//If num2 is not final, the local variable disappears after the call to method () is completed, but the new Inner in heap memory does not disappear immediately A Public voidShow () {///and the object New Inner () is still using num2, but as mentioned earlier num2 has disappeared prematurely, so it will go wrong, add final at System.out.println (num); - //Access local variable num2 from inner class; need to be declared as final type -System.out.println (NUM2);//after---decompile you will see that the final effect is System.out.println (20), replacing the num2 directly with - } - } - in //System.out.println (num2); - toInner i =NewInner (); + i.show (); - } the } * $ classInnerClassDemo5 {Panax Notoginseng Public Static voidMain (string[] args) { -Outer o =NewOuter (); the O.method (); + } A}
Android (Java) Learning Note 112: Introduction to the internal classes of local locations