Hualing
Hualing

Reputation: 21

Local variable Table In JVM Stack

Today I compile a class to bytecode, and I can't find anything in main method local variable table index 0, It's start from 1, I know a non-static method start 1 because it's 'this' at 0.

Here is the bytecode.

  // this is the main method
  public static void main(java.lang.String[]);
    Code:
       0: iconst_1
       1: istore_1   // index: 1 (what is content at index 0 ?)
       2: iconst_2
       3: istore_2
       4: return

  // this is a static method
  public static void staticMethod();
    Code:
       0: iconst_1
       1: istore_0  // index: 0 (no 'this')
       2: iconst_2
       3: istore_1
       4: return

  // this is a non-static method
  public void nonStaticMethod();
    Code:
       0: iconst_1
       1: istore_1  // index: 1  (index 0 should be 'this')
       2: iconst_1
       3: istore_2
       4: return

Help me please, Thanks!

Upvotes: 2

Views: 1558

Answers (1)

Joni
Joni

Reputation: 111259

The local variable at index 0 in the main method is the method's argument: a reference to String[]. From the JVM specification:

The Java Virtual Machine uses local variables to pass parameters on method invocation. On class method invocation, any parameters are passed in consecutive local variables starting from local variable 0.

https://docs.oracle.com/javase/specs/jvms/se14/html/jvms-2.html#jvms-2.6.1

Upvotes: 4

Related Questions