Scenario One: Anonymous inner class, non-static inner class implicitly holds references to external classes example of a non-static inner class
1 Public classTest {2 Private intTestValue = 1;3 4 Public classOuter {5 Private intOutervalue = 2;6 7 Public classInner {8 Private intInnervalue = 3;9 Ten Public voidprint () { OneSystem.out.println ("testvalue=" + TestValue + ", outervalue=" +Outervalue A+ ", innervalue=" +innervalue); - } - } the } - - Public Static voidMain (string[] args) { -Test test =NewTest (); +Outer Outer = Test.NewOuter (); -Outer.Inner Inner = Outer.NewInner (); + inner.print (); A } at}
As shown in line 19-20, when you instantiate a non-static inner class outside a class, you need to construct it through an instance of the outer class. New.
Compile with Javac Test.java to generate Test$outer$inner.class, Test$outer.class, test.class several class files. Use Javap-p class_file_name to view the results as follows:
? ~/programming/java javap-p "Test.class"Compiled from"Test.java" Public classTest {Private intTestValue; PublicTest (); Public Static voidMain (java.lang.string[]); Static intaccess$000(Test);}? ~/programming/java javap-p "test\ $Outer. Class"Compiled from"Test.java" Public classTest$outer {Private intOutervalue; FinalTest This$; PublicTest$outer (Test); Static intaccess$100(Test$outer);}? ~/programming/java javap-p "test\ $Outer \ $Inner. Class"Compiled from"Test.java" Public classTest$outer$inner {Private intInnervalue; FinalTest$outer This$; PublicTest$outer$inner (Test$outer); Public voidprint ();}
As can be seen from the generated class file, the constructor of the inner class is automatically added to the parameter as a class object, and the inner classes have a member pointing to an Outer class object (Test$outer's This$0,test$outer$inner this$1).
External classes and inner classes can access each other's private properties, and each class is a separate class file after compilation, so the compiler automatically generates a static method of the appropriate package access (which is generated only when needed). To support access to this private property (the access$000 static method generated for TestValue in Test, access$100 static method generated for Outervalue in Test$outer).
Anonymous Inner class example
1 Public classAnonymoustest {2 Private intTestValue = 1;3 4 Public Interfaceianonymous {5 voidprint ();6 }7 8 Private Finalianonymous anonymous =Newianonymous () {9 Private intInnervalue = 2;Ten One Public voidprint () { ASystem.out.println ("testvalue=" + TestValue + ", innervalue=" +innervalue); - } - }; the - Public Static voidMain (string[] args) { -Anonymoustest test =Newanonymoustest (); - test.anonymous.print (); + } -}
Compile with Javac Anonymoustest.java to generate Anonymoustest$1.class, Anonymoustest$ianonymous.class, Anonymoustest.class several class files. Use Javap-p class_file_name to view the results as follows:
? ~/programming/java javap-p anonymoustest.classCompiled from"Anonymoustest.java" Public classAnonymoustest {Private intTestValue; Private FinalAnonymoustest$ianonymous Anonymous; Publicanonymoustest (); Public Static voidMain (java.lang.string[]); Static intaccess$000(anonymoustest);}? ~/programming/java javap-p "anonymoustest\ $IAnonymous. Class"Compiled from"Anonymoustest.java" Public Interfaceanonymoustest$ianonymous { Public Abstract voidprint ();}? ~/programming/java javap-p "Anonymoustest\$1.class"Compiled from"Anonymoustest.java"classAnonymoustest$1Implementsanonymoustest$ianonymous {Private intInnervalue; FinalAnonymoustest This$; anonymoustest$1(anonymoustest); Public voidprint ();}
As you can see from the generated class file, anonymous inner classes, like non-static inner classes, also hold references to external classes. The difference is that the name of the anonymous inner class is the outer class $ number. Anonymous inner classes can define member variables and member functions, but they are accessible only in this class, and new instances of anonymous inner classes cannot be constructed directly from Java code.
Conclusion
Because an instance of a non-static inner class and an anonymous inner class implicitly holds a reference to an instance of an external class, a leak occurs if its life cycle is longer than the external class instance, causing the external class instance to fail to recycle. Therefore, you should pay extra attention to the use.
Android Memory leak scenario (1)