为什么非静态方法有额外的Local?还有,这是什么?

时间:2011-05-02 09:23:54

标签: java jvm bytecode

根据我对方法1中定义的每个局部变量的理解,为此Locals指标添加了local。

来源:

public class A {
    public void a() {}

    public static void b() {}
}

生成的字节码:

Compiled from "A.java"
public class A extends java.lang.Object
  SourceFile: "A.java"
  minor version: 0
  major version: 50
  Constant pool:
const #1 = Method       #3.#12; //  java/lang/Object."<init>":()V
const #2 = class        #13;    //  A
const #3 = class        #14;    //  java/lang/Object
const #4 = Asciz        <init>;
const #5 = Asciz        ()V;
const #6 = Asciz        Code;
const #7 = Asciz        LineNumberTable;
const #8 = Asciz        a;
const #9 = Asciz        b;
const #10 = Asciz       SourceFile;
const #11 = Asciz       A.java;
const #12 = NameAndType #4:#5;//  "<init>":()V
const #13 = Asciz       A;
const #14 = Asciz       java/lang/Object;

{
public A();
  Code:
   Stack=1, Locals=1, Args_size=1
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return
  LineNumberTable:
   line 1: 0


public void a();
  Code:
   Stack=0, Locals=1, Args_size=1
   0:   return
  LineNumberTable:
   line 6: 0


public static void b();
  Code:
   Stack=0, Locals=0, Args_size=0
   0:   return
  LineNumberTable:
   line 11: 0


}

2 个答案:

答案 0 :(得分:6)

这是当前的对象,a.k.a。this。来自the JVM Spec §3.6.1 Local Variables

  

在实例方法调用中,局部变量0总是用于传递对调用实例方法的对象的引用(Java编程语言中为this)。

答案 1 :(得分:3)

我的猜测是this