invokevirtual 0xb6
Calls an instance method, chosen by the receiver's class.
- Opcode
0xb6(182)- Family
- Method calls
- Operands
- 2 bytes: constant pool index
- Length
- 3 bytes
- Operand stack
…, objectref, arg1, … → …, result- Can throw
NullPointerException,AbstractMethodError
What it does
Calls an instance method, chosen at run time by the receiver's actual class: ordinary virtual dispatch. Most calls you write on objects compile to it.
Before Java 9, "a" + b compiled to a chain of StringBuilder.append invokevirtuals; now it is one invokedynamic. If the method returns void, nothing is pushed. Long and double arguments take two stack slots each.
How often javac uses it
In JDK 25.0.5's java.base module, invokevirtual appears 118,921 times in 4,677 of its 7,391 classes: 6.1% of all instructions, the 3rd most common of 194 opcodes in use. Some of the methods that use it: java.io.BufferedInputStream.getBufIfOpen, java.io.BufferedOutputStream.initialBufferSize, java.io.BufferedReader.fill, java.io.BufferedReader$1.hasNext.
Examples
From the sample classes, compiled by javac 25 with -g. Each Java line is shown above the bytecode it compiled to; invokevirtual is highlighted.
public static EnumSwitch$Size[] values() in EnumSwitch.Size, compiled by javac 25enum Size { SMALL, MEDIUM, LARGE }private static void lambda$hello$0() synthetic in Lambda, compiled by javac 25return () -> System.out.println("hello");Related instructions
invokeinterface Calls an interface method, chosen by the receiver's class.invokespecial Calls a constructor, a private method or a superclass method directly.invokestatic Calls a static method.
Specification: JVMS §6.5, invokevirtual (Java SE 25).