Sub topic 7
How methods are invoked: `invokestatic`, `invokevirtual` and friends
Assume you can write and run Java programs, understand classes and interfaces, and have used javac, java, and basic javap commands. This chapter focuses on how the JVM selects and calls a method after the compiler has produced bytecode.
The invocation instructions
Java bytecode uses different instructions for different method relationships:
| Instruction | Typical target | How the target is selected |
|---|---|---|
| `invokestatic` | A static method | The referenced class and method are selected directly |
| `invokevirtual` | An ordinary class method | The runtime object's class can select an override |
| `invokeinterface` | An interface method | The runtime object's class supplies the implementation |
| `invokespecial` | A constructor, private method, or `super` call | The compiler selects a specific special method |
| `invokedynamic` | Dynamically linked behavior, such as lambdas | A bootstrap method supplies the call target |
The first four instructions are common in ordinary object-oriented code. invokedynamic supports language features and libraries that need a call target to be linked at runtime.
A small example
interface Greeter {
void greet();
}
class Parent {
void print() {
System.out.println("parent");
}
}
class Child extends Parent implements Greeter {
private final String name;
Child(String name) {
this.name = name;
}
@Override
public void greet() {
System.out.println("hello " + name);
}
@Override
void print() {
System.out.println("child");
}
}
public class InvokeDemo {
static void announce() {
System.out.println("announcement");
}
public static void main(String[] args) {
announce();
Child child = new Child("Mina");
child.print();
Greeter greeter = child;
greeter.greet();
child.callParent();
}
}The example needs callParent, so add this method to Child:
void callParent() {
super.print();
}Compile and inspect the caller:
$ javac InvokeDemo.java
$ javap -c InvokeDemo ChildThe call to announce normally becomes invokestatic. The call to child.print() becomes invokevirtual, because print is an overridable class method. The call through greeter becomes invokeinterface. The constructor call uses invokespecial, as does super.print().
How the operand stack supplies arguments
Before an invocation, bytecode places the receiver and arguments on the operand stack. A receiver is the object on which an instance method runs.
Conceptually, this source statement:
child.print();becomes something like:
aload_1 // push child onto the operand stack
invokevirtual Child.print:()VFor a method with arguments, the stack contains the receiver first, followed by the arguments. The invocation instruction removes these values and creates a new stack frame for the called method. The frame contains the method's local variables, operand stack, and return information.
A static call has no receiver:
invokestatic InvokeDemo.announce:()VThe method descriptor ()V means “takes no arguments and returns void.” A descriptor such as (I)I means “takes one integer and returns an integer.”
Reading a call
When inspecting javap -c output, identify these items:
- Which invocation instruction appears.
- What values the caller pushed onto the operand stack.
- Whether the method returns a value.
- Whether overriding or interface dispatch can change the implementation.
Exercise
Which instruction is normally used for a call to a static method, and why does it not need an object reference?
Show answer
invokestatic is used. A static method belongs to its class rather than to a particular object, so the operand stack does not need a receiver.