The ASM guide talks about constructors:
package pkg; public class Bean { private int f; public int getF() { return this.f; } public void setF(int f) { this.f = f; } }
The Bean class also has a default public constructor which is generated by the compiler, since no explicit constructor was defined by the programmer. This default public constructor is generated as
Bean() { super(); }
. The bytecode of this constructor is the following:ALOAD 0 INVOKESPECIAL java/lang/Object <init> ()V RETURN
The first instruction pushes
this
on the operand stack. The second instruction pops this value from the stack, and calls the<init>
method defined in theObject
class. This corresponds to thesuper()
call, i.e. a call to the constructor of the super class,Object
. You can see here that constructors are named differently in compiled and source classes: in compiled classes they are always named<init>
, while in source classes they have the name of the class in which they are defined. Finally the last instruction returns to the caller.
How is the value of this
already known to the JVM before the first instruction of the constructor?