Before that, we have discussed how the members ' inner classes can access the members of the external class unconditionally, and how exactly is it implemented? Here's a look at what to do by deserializing the bytecode file. In fact, when compiling, the compiler compiles the member internals into a single bytecode file, the following is the code for Outter.java:
Public classOutter {PrivateInner Inner =NULL; Publicoutter () {} PublicInner getinnerinstance () {if(Inner = =NULL) Inner=NewInner (); returninner; } protected classInner { PublicInner () {} }}
After compiling, two bytecode files appear:
The decompile outter$inner.class file gets the following information:
E:\WORKSPACE\TEST\BIN\COM\CXH\TEST2>JAVAP-v outter$innercompiled from"Outter.java" Public classCom.cxh.test2.outter$innerextendsjava.lang.Object sourcefile:"Outter.java"Innerclass: #24= #1 of #22;//Inner=class Com/cxh/test2/outter$inner of class Com/cxh/test2/outter minor version:0Major Version:50Constant Pool:Const#1 =class#2;//Com/cxh/test2/outter$innerConst#2 = Asciz com/cxh/test2/Outter$inner;Const#3 =class#4;//Java/lang/objectConst#4 = Asciz java/lang/Object;Const#5 = Asciz This$;Const#6 = Asciz lcom/cxh/test2/outter;;Const#7 = Asciz <init>;Const#8 = Asciz (lcom/cxh/test2/outter;) V;Const#9 =Asciz Code;Const#10 = Field #1. #11;//com/cxh/test2/outter$inner.this$0:lcom/cxh/test2/Outter;Const#11 = Nameandtype #5: #6;//This$0:lcom/cxh/test2/outter;Const#12 = Method #3. #13;//java/lang/object. " <init> ":() VConst#13 = Nameandtype #7: #14;//"<init>":() VConst#14 =Asciz () V;Const#15 =Asciz linenumbertable;Const#16 =Asciz localvariabletable;Const#17 = Asciz This;Const#18 = Asciz lcom/cxh/test2/Outter$inner;;Const#19 =Asciz sourcefile;Const#20 =Asciz Outter.java;Const#21 =Asciz innerclasses;Const#22 =class#23;//Com/cxh/test2/outterConst#23 = Asciz com/cxh/test2/Outter;Const#24 =Asciz Inner; {FinalCom.cxh.test2.Outter This$; PublicCom.cxh.test2.outter$inner (Com.cxh.test2.Outter); Code:stack=2, locals=2, args_size=2 0: Aload_01: Aload_12:putfield #10;//Field This$0:lcom/cxh/test2/outter;5: Aload_06:invokespecial #12;//Method java/lang/object. " <init> ":() V9:returnLinenumbertable:line16:0 Line18:9localvariabletable:start Length Slot Name Signature0 10 0 Thislcom/cxh/test2/Outter$inner; }
Rows 11th to 35 are the contents of a constant pool, and the following is the contents of the 38th line:
Final this $;
This line is a pointer to the outer class object, see here presumably everyone enlightened. This means that the compiler defaults to adding a reference to the Outer class object to the member's inner class, so how does this reference assign an initial value? The following looks at the constructor of the inner class:
public Com.cxh.test2.outter$inner (Com.cxh.test2.Outter);
As can be seen here, although the constructor of the inner class that we define is the parameterless constructor, the compiler will add a parameter by default, which is a reference to an external class object, so the outter this&0 pointer in the member's inner class points to the Outer class object. Therefore, you can freely access members of external classes within a member's inner class. It is also implied from this that the member inner class is dependent on the outer class, and if no object of the outer class is created, the Outter this&0 reference cannot be initialized and the object of the member's inner class cannot be created.
Java Basics-Inner Classes-why the member inner class can access the outer class unconditionally