2012-04-05 60 views
1

第一步:我編譯如下代碼:爲什麼沒有在內部類構造函數外部類對象LocalVariableTable條目(Java字節碼)

public class OuterClass { 
class InnerClass{ 

    } 
} 

我有OuterClass.class和OterClass $ InnerClass.class

第2步:我使用javap來檢查內部類的字節碼。構造函數方法使用「1:aload_1」非常奇怪,但在LocalVariableTable中,只有一個可以通過aload_0訪問的「this」條目。我假定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; 


} 

回答

0

我認爲它只是因爲它並不需要在局部變量表中的條目,是永遠不會需要比作爲參數任何其他方式來獲得所引用的論據。

1

LocalVariableTable純粹是爲了調試目的 - 它對執行沒有影響。因此,它只包含源可見變量,因爲這是調試器感興趣的內容。沒有理由包含編譯器生成的變量。

相關問題