What does getstatic mean for bytecode?

I have this byte code:

new java.lang.Object // stack is [newObjectRef] dup // Stack is [newObjectRef newObjectRef] invokespecial void java.lang.Object.<init>() // Stack is [initializedAsTypeObjectObjectRef] putstatic java.lang.Object class.a // variable a has the reference of new object getstatic java.io.PrintStream java.lang.System.out // Take the static value of System.out // Stack is [initializedAsTypeObjectObjectRef System.out] 

The update is a continuation:

 > ldc "test" // Stack is > [initializedAsTypeObjectObjectRef System.out "test"] > jsr pos.0000026C // call a subrutine invokevirtual void > java.io.PrintStream.println(java.lang.String) // actually print the > result // stack is (I think) Empty at this time ? 

Is there a translation:

  Object a = new Object(); a = "test"; System.out.print(a); 

Is my stack good?

I'm not sure I understand well (). May I have to use set () setter and print () after?

I always use out () for regular printing. A.

+4
source share
1 answer

If I compile the code

 public static void main(String[] args) { Object a; a = "test"; System.out.println(a); } 

and run

 javap -c Main 

I see

 public static void main(java.lang.String[]); Code: 0: ldc #2 // String test 2: astore_1 3: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream; 6: aload_1 7: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V 10: return 

You can see getstatic loading the System.out field


The object does not have a method called out() , so I do not believe that you are looking at the code that you think.

getstatic gets static fields, for example. System.out is the static field of the system, so if you write

 System.out.println(); 

This will result in using getstatic

+3
source

Source: https://habr.com/ru/post/1386835/


All Articles