---
spec: JVMS
title: "The Java Virtual Machine Specification"
edition: "Java SE 16 Edition"
java_version: 16
chapter: "Chapter 6. The Java Virtual Machine Instruction Set"
section: "6.5.invokevirtual"
heading: "invokevirtual"
parent: "6.5. Instructions"
source: https://docs.oracle.com/javase/specs/jvms/se16/html/jvms-6.html#jvms-6.5.invokevirtual
id: "6.5.invokevirtual"
url: https://jvm.obfuscated.software/jvms/se16/6.5.invokevirtual.html
raw: https://jvm.obfuscated.software/jvms/se16/6.5.invokevirtual.md
---

# invokevirtual

## Operation

Invoke instance method; dispatch based on class

## Format

```
invokevirtual
indexbyte1
indexbyte2
```

## Forms

*invokevirtual* = 182 (0xb6)

## Operand Stack

..., *objectref*, [*arg1*, [*arg2* ...]] →

...

## Description

The unsigned *indexbyte1* and *indexbyte2* are used to construct an index into the run-time constant pool of the current class ([§2.6](2.6.md)), where the value of the index is (*indexbyte1* `<<` 8) | *indexbyte2*. The run-time constant pool entry at the index must be a symbolic reference to a method ([§5.1](5.1.md)), which gives the name and descriptor ([§4.3.3](4.3.3.md)) of the method as well as a symbolic reference to the class in which the method is to be found. The named method is resolved ([§5.4.3.3](5.4.3.3.md)).

*If the resolved method is not signature polymorphic* ([§2.9.3](2.9.3.md)), then the *invokevirtual* instruction proceeds as follows.

Let C be the class of *objectref*. A method is selected with respect to C and the resolved method ([§5.4.6](5.4.6.md)). This is the *method to be invoked*.

The *objectref* must be followed on the operand stack by *nargs* argument values, where the number, type, and order of the values must be consistent with the descriptor of the selected instance method.

If the method to be invoked is `synchronized`, the monitor associated with *objectref* is entered or reentered as if by execution of a *monitorenter* instruction ([§*monitorenter*](6.5.monitorenter.md)) in the current thread.

If the method to be invoked is not `native`, the *nargs* argument values and *objectref* are popped from the operand stack. A new frame is created on the Java Virtual Machine stack for the method being invoked. The *objectref* and the argument values are consecutively made the values of local variables of the new frame, with *objectref* in local variable 0, *arg1* in local variable 1 (or, if *arg1* is of type `long` or `double`, in local variables 1 and 2), and so on. Any argument value that is of a floating-point type undergoes value set conversion ([§2.8.3](2.8.3.md)) prior to being stored in a local variable. The new frame is then made current, and the Java Virtual Machine `pc` is set to the opcode of the first instruction of the method to be invoked. Execution continues with the first instruction of the method.

If the method to be invoked is `native` and the platform-dependent code that implements it has not yet been bound ([§5.6](5.6.md)) into the Java Virtual Machine, that is done. The *nargs* argument values and *objectref* are popped from the operand stack and are passed as parameters to the code that implements the method. Any argument value that is of a floating-point type undergoes value set conversion ([§2.8.3](2.8.3.md)) prior to being passed as a parameter. The parameters are passed and the code is invoked in an implementation-dependent manner. When the platform-dependent code returns, the following take place:

- If the `native` method is `synchronized`, the monitor associated with *objectref* is updated and possibly exited as if by execution of a *monitorexit* instruction ([§*monitorexit*](6.5.monitorexit.md)) in the current thread.
- If the `native` method returns a value, the return value of the platform-dependent code is converted in an implementation-dependent way to the return type of the `native` method and pushed onto the operand stack.

*If the resolved method is signature polymorphic* ([§2.9.3](2.9.3.md)), *and declared in the `java.lang.invoke.MethodHandle` class*, then the *invokevirtual* instruction proceeds as follows, where D is the descriptor of the method symbolically referenced by the instruction.

First, a `reference` to an instance of `java.lang.invoke.MethodType` is obtained as if by resolution of a symbolic reference to a method type ([§5.4.3.5](5.4.3.5.md)) with the same parameter and return types as D.

- If the named method is `invokeExact`, the instance of `java.lang.invoke.MethodType` must be semantically equal to the type descriptor of the receiving method handle *objectref*. The *method handle to be invoked* is *objectref*.
- If the named method is `invoke`, and the instance of `java.lang.invoke.MethodType` is semantically equal to the type descriptor of the receiving method handle *objectref*, then the *method handle to be invoked* is *objectref*.
- If the named method is `invoke`, and the instance of `java.lang.invoke.MethodType` is not semantically equal to the type descriptor of the receiving method handle *objectref*, then the Java Virtual Machine attempts to adjust the type descriptor of the receiving method handle, as if by invocation of the `asType` method of `java.lang.invoke.MethodHandle`, to obtain an exactly invokable method handle `m`. The *method handle to be invoked* is `m`.

The *objectref* must be followed on the operand stack by *nargs* argument values, where the number, type, and order of the values must be consistent with the type descriptor of the method handle to be invoked. (This type descriptor will correspond to the method descriptor appropriate for the kind of the method handle to be invoked, as specified in [§5.4.3.5](5.4.3.5.md).)

Then, if the method handle to be invoked has bytecode behavior, the Java Virtual Machine invokes the method handle as if by execution of the bytecode behavior associated with the method handle's kind. If the kind is 5 (`REF_invokeVirtual`), 6 (`REF_invokeStatic`), 7 (`REF_invokeSpecial`), 8 (`REF_newInvokeSpecial`), or 9 (`REF_invokeInterface`), then a frame will be created and made current *in the course of executing the bytecode behavior*; however, this frame is not visible, and when the method invoked by the bytecode behavior completes (normally or abruptly), the *frame of its invoker* is considered to be the frame for the method containing this *invokevirtual* instruction.

Otherwise, if the method handle to be invoked has no bytecode behavior, the Java Virtual Machine invokes it in an implementation-dependent manner.

*If the resolved method is signature polymorphic and declared in the `java.lang.invoke.VarHandle` class*, then the *invokevirtual* instruction proceeds as follows, where `N` and D are the name and descriptor of the method symbolically referenced by the instruction.

First, a `reference` to an instance of `java.lang.invoke.VarHandle.AccessMode` is obtained as if by invocation of the `valueFromMethodName` method of `java.lang.invoke.VarHandle.AccessMode` with a `String` argument denoting `N`.

Second, a `reference` to an instance of `java.lang.invoke.MethodType` is obtained as if by invocation of the `accessModeType` method of `java.lang.invoke.VarHandle` on the instance *objectref*, with the instance of `java.lang.invoke.VarHandle.AccessMode` as the argument.

Third, a `reference` to an instance of `java.lang.invoke.MethodHandle` is obtained as if by invocation of the `varHandleExactInvoker` method of `java.lang.invoke.MethodHandles` with the instance of `java.lang.invoke.VarHandle.AccessMode` as the first argument and the instance of `java.lang.invoke.MethodType` as the second argument. The resulting instance is called the *invoker method handle*.

Finally, the *nargs* argument values and *objectref* are popped from the operand stack, and the invoker method handle is invoked. The invocation occurs as if by execution of an *invokevirtual* instruction that indicates a run-time constant pool index to a symbolic reference R where:

- R is a symbolic reference to a method of a class;
- for the symbolic reference to the class in which the method is to be found, R specifies `java.lang.invoke.MethodHandle`;
- for the name of the method, R specifies `invoke`;
- for the descriptor of the method, R specifies a return type indicated by the return descriptor of D, and specifies a first parameter type of `java.lang.invoke.VarHandle` followed by the parameter types indicated by the parameter descriptors of D (if any) in order.

and where it is as if the following items were pushed, in order, onto the operand stack:

- a `reference` to the instance of `java.lang.invoke.MethodHandle` (the invoker method handle);
- *objectref*;
- the *nargs* argument values, where the number, type, and order of the values must be consistent with the type descriptor of the invoker method handle.

## Linking Exceptions

During resolution of the symbolic reference to the method, any of the exceptions pertaining to method resolution ([§5.4.3.3](5.4.3.3.md)) can be thrown.

Otherwise, if the resolved method is a class (`static`) method, the *invokevirtual* instruction throws an `IncompatibleClassChangeError`.

Otherwise, if the resolved method is signature polymorphic and declared in the `java.lang.invoke.MethodHandle` class, then during resolution of the method type derived from the descriptor in the symbolic reference to the method, any of the exceptions pertaining to method type resolution ([§5.4.3.5](5.4.3.5.md)) can be thrown.

Otherwise, if the resolved method is signature polymorphic and declared in the `java.lang.invoke.VarHandle` class, then any linking exception that may arise from invocation of the invoker method handle can be thrown. No linking exceptions are thrown from invocation of the `valueFromMethodName`, `accessModeType`, and `varHandleExactInvoker` methods.

## Run-time Exceptions

Otherwise, if *objectref* is `null`, the *invokevirtual* instruction throws a `NullPointerException`.

Otherwise, if the resolved method is not signature polymorphic:

- If the selected method is `abstract`, *invokevirtual* throws an `AbstractMethodError`.
- Otherwise, if the selected method is `native` and the code that implements the method cannot be bound, *invokevirtual* throws an `UnsatisfiedLinkError`.
- Otherwise, if no method is selected, and there are multiple maximally-specific superinterface methods of C that match the resolved method's name and descriptor and are not `abstract`, *invokevirtual* throws an `IncompatibleClassChangeError`
- Otherwise, if no method is selected, and there are no maximally-specific superinterface methods of C that match the resolved method's name and descriptor and are not `abstract`, *invokevirtual* throws an `AbstractMethodError`.

Otherwise, if the resolved method is signature polymorphic and declared in the `java.lang.invoke.MethodHandle` class, then:

- If the method name is `invokeExact`, and the obtained instance of `java.lang.invoke.MethodType` is not semantically equal to the type descriptor of the receiving method handle *objectref*, the *invokevirtual* instruction throws a `java.lang.invoke.WrongMethodTypeException`.
- If the method name is `invoke`, and the obtained instance of `java.lang.invoke.MethodType` is not a valid argument to the `asType` method of `java.lang.invoke.MethodHandle` invoked on the receiving method handle *objectref*, the *invokevirtual* instruction throws a `java.lang.invoke.WrongMethodTypeException`.

Otherwise, if the resolved method is signature polymorphic and declared in the `java.lang.invoke.VarHandle` class, then any run-time exception that may arise from invocation of the invoker method handle can be thrown. No run-time exceptions are thrown from invocation of the `valueFromMethodName`, `accessModeType`, and `varHandleExactInvoker` methods, except `NullPointerException` if *objectref* is `null`.

## Notes

The *nargs* argument values and *objectref* are not one-to-one with the first *nargs*+1 local variables. Argument values of types `long` and `double` must be stored in two consecutive local variables, thus more than *nargs* local variables may be required to pass *nargs* argument values to the invoked method.

It is possible that the symbolic reference of an *invokevirtual* instruction resolves to an interface method. In this case, it is possible that there is no overriding method in the class hierarchy, but that a non-`abstract` interface method matches the resolved method's descriptor. The selection logic matches such a method, using the same rules as for *invokeinterface*.
