Step1:我编译了以下代码:
public class OuterClass {
class InnerClass{
}
}
我有 OuterClass.class 和 OterClass$InnerClass.class
Step2:我使用javap检查内部类的字节码。很奇怪,构造方法使用了“1:aload_1”,但是在LocalVariableTable中,只有一个“this”的入口可以被aload_0访问。我假设 aload_1 是将传入参数(外部类对象的引用)加载到堆栈顶部。
我的问题:为什么编译器不为其创建一个条目(传递给构造函数的外部类的引用)?
我尝试使用 Jsdk 1.4 和 openjdk 1.6。
~$ javap -v -c OuterClass\$InnerClass
Compiled from "OuterClass.java"
class OuterClass$InnerClass extends java.lang.Object
SourceFile: "OuterClass.java"
InnerClass:
#24= #1 of #22; //InnerClass=class OuterClass$InnerClass of class OuterClass
minor version: 0
major version: 50
Constant pool:
const #1 = class #2; // OuterClass$InnerClass
const #2 = Asciz OuterClass$InnerClass;
const #3 = class #4; // java/lang/Object
const #4 = Asciz java/lang/Object;
const #5 = Asciz this$0;
const #6 = Asciz LOuterClass;;
const #7 = Asciz <init>;
const #8 = Asciz (LOuterClass;)V;
const #9 = Asciz Code;
const #10 = Field #1.#11; // OuterClass$InnerClass.this$0:LOuterClass;
const #11 = NameAndType #5:#6;// this$0:LOuterClass;
const #12 = Method #3.#13; // java/lang/Object."<init>":()V
const #13 = NameAndType #7:#14;// "<init>":()V
const #14 = Asciz ()V;
const #15 = Asciz LineNumberTable;
const #16 = Asciz LocalVariableTable;
const #17 = Asciz this;
const #18 = Asciz LOuterClass$InnerClass;;
const #19 = Asciz SourceFile;
const #20 = Asciz OuterClass.java;
const #21 = Asciz InnerClasses;
const #22 = class #23; // OuterClass
const #23 = Asciz OuterClass;
const #24 = Asciz InnerClass;
{
final OuterClass this$0;
OuterClass$InnerClass(OuterClass);
Code:
Stack=2, Locals=2, Args_size=2
0: aload_0
1: aload_1
2: putfield #10; //Field this$0:LOuterClass;
5: aload_0
6: invokespecial #12; //Method java/lang/Object."<init>":()V
9: return
LineNumberTable:
line 3: 0
LocalVariableTable:
Start Length Slot Name Signature
0 10 0 this LOuterClass$InnerClass;
}