为什么非静态方法有额外的Local?那是什么?

发布于 2024-11-04 20:19:25 字数 1315 浏览 4 评论 0原文

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

来源:

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


}

As per my understanding for each and every local varible define in method 1 local is added for this Locals indicator.

Source :

public class A {
    public void a() {}

    public static void b() {}
}

Generated bytecode :

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


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千纸鹤带着心事 2024-11-11 20:19:25

它是当前对象,又名 this。来自 JVM 规范第 §3.6.1 局部变量< /a>:

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

It is the current object, a.k.a. this. From the JVM Spec §3.6.1 Local Variables:

On instance method invocation, local variable 0 is always used to pass a reference to the object on which the instance method is being invoked (this in the Java programming language).

叹沉浮 2024-11-11 20:19:25

我的猜测是这个

My guess would be that it's this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文