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

# invokedynamic

## Operation

Invoke dynamic method

## Format

```
invokedynamic
indexbyte1
indexbyte2
0
0
```

## Forms

*invokedynamic* = 186 (0xba)

## Operand Stack

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

...

## Description

Each specific lexical occurrence of an *invokedynamic* instruction is called a *dynamic call site*.

First, 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 item at that index must be a symbolic reference to a call site specifier ([§5.1](5.1.md)). The values of the third and fourth operand bytes must always be zero.

The call site specifier is resolved ([§5.4.3.6](5.4.3.6.md)) *for this specific dynamic call site* to obtain a `reference` to a `java.lang.invoke.MethodHandle` instance that will serve as the bootstrap method, a `reference` to a `java.lang.invoke.MethodType` instance, and `reference`s to static arguments.

Next, as part of the continuing resolution of the call site specifier, the bootstrap method is invoked as if by execution of an *invokevirtual* instruction ([§*invokevirtual*](6.5.invokevirtual.md)) that contains a run-time constant pool index to a symbolic reference R where:

- R is a symbolic reference to a method of a class ([§5.1](5.1.md));

- 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 of `java.lang.invoke.CallSite` and parameter types derived from the items pushed on the operand stack.

  The first three parameter types are `java.lang.invoke.MethodHandles.Lookup`, `String`, and `java.lang.invoke.MethodType`, in that order. If the call site specifier has any static arguments, then a parameter type for each argument is appended to the parameter types of the method descriptor in the order that the arguments were pushed on to the operand stack. These parameter types may be `Class`, `java.lang.invoke.MethodHandle`, `java.lang.invoke.MethodType`, `String`, `int`, `long`, `float`, or `double`.

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

- the `reference` to the `java.lang.invoke.MethodHandle` object for the bootstrap method;
- a `reference` to a `java.lang.invoke.MethodHandles.Lookup` object for the class in which this dynamic call site occurs;
- a `reference` to the `String` for the method name in the call site specifier;
- the `reference` to the `java.lang.invoke.MethodType` object obtained for the method descriptor in the call site specifier;
- `reference`s to classes, method types, method handles, and string literals denoted as static arguments in the call site specifier, and numeric values ([§2.3.1](2.3.1.md), [§2.3.2](2.3.2.md)) denoted as static arguments in the call site specifier, in the order in which they appear in the call site specifier. (That is, no boxing occurs for primitive values.)

The symbolic reference R describes a method which is signature polymorphic ([§2.9](2.9.md)). Due to the operation of *invokevirtual* on a signature polymorphic method called `invoke`, the type descriptor of the receiving method handle (representing the bootstrap method) need not be semantically equal to the method descriptor specified by R. For example, the first parameter type specified by R could be `Object` instead of `java.lang.invoke.MethodHandles.Lookup`, and the return type specified by R could be `Object` instead of `java.lang.invoke.CallSite`. As long as the bootstrap method can be invoked by the `invoke` method without a `java.lang.invoke.WrongMethodTypeException` being thrown, the type descriptor of the method handle which represents the bootstrap method is arbitrary.

If the bootstrap method is a variable arity method, then some or all of the arguments on the operand stack specified above may be collected into a trailing array parameter.

The invocation of a bootstrap method occurs within a thread that is attempting resolution of the symbolic reference to the call site specifier *of this dynamic call site*. If there are several such threads, the bootstrap method may be invoked in several threads concurrently. Therefore, bootstrap methods which access global application data must take the usual precautions against race conditions.

The result returned by the bootstrap method must be a `reference` to an object whose class is `java.lang.invoke.CallSite` or a subclass of `java.lang.invoke.CallSite`. This object is known as the *call site object*. The `reference` is popped from the operand stack used as if in the execution of an *invokevirtual* instruction.

If several threads simultaneously execute the bootstrap method for the same dynamic call site, the Java Virtual Machine must choose one returned call site object and install it visibly to all threads. Any other bootstrap methods executing for the dynamic call site are allowed to complete, but their results are ignored, and the threads' execution of the dynamic call site proceeds with the chosen call site object.

The call site object has a type descriptor (an instance of `java.lang.invoke.MethodType`) which must be semantically equal to the `java.lang.invoke.MethodType` object obtained for the method descriptor in the call site specifier.

The result of successful call site specifier resolution is a call site object which is permanently bound to the dynamic call site.

The method handle represented by the target of the bound call site object is invoked. The invocation occurs as if by execution of an *invokevirtual* instruction ([§*invokevirtual*](6.5.invokevirtual.md)) that indicates a run-time constant pool index to a symbolic reference to a method ([§5.1](5.1.md)) with the following properties:

- The method's name is `invokeExact`;
- The method's descriptor is the method descriptor in the call site specifier; and
- The method's symbolic reference to the class in which the method is to be found indicates the class `java.lang.invoke.MethodHandle`.

The operand stack will be interpreted as containing a `reference` to the target of the call site object, followed by *nargs* argument values, where the number, type, and order of the values must be consistent with the method descriptor in the call site specifier.

## Linking Exceptions

If resolution of the symbolic reference to the call site specifier throws an exception E, the *invokedynamic* instruction throws a `BootstrapMethodError` that wraps E.

Otherwise, during the continuing resolution of the call site specifier, if invocation of the bootstrap method completes abruptly ([§2.6.5](2.6.5.md)) because of a throw of exception E, the *invokedynamic* instruction throws a `BootstrapMethodError` that wraps E. (This can occur if the bootstrap method has the wrong arity, parameter type, or return type, causing `java.lang.invoke.MethodHandle` `.` `invoke` to throw `java.lang.invoke.WrongMethodTypeException`.)

Otherwise, during the continuing resolution of the call site specifier, if the result from the bootstrap method invocation is not a `reference` to an instance of `java.lang.invoke.CallSite`, the *invokedynamic* instruction throws a `BootstrapMethodError`.

Otherwise, during the continuing resolution of the call site specifier, if the type descriptor of the target of the call site object is not semantically equal to the method descriptor in the call site specifier, the *invokedynamic* instruction throws a `BootstrapMethodError`.

## Run-time Exceptions

If this specific dynamic call site completed resolution of its call site specifier, it implies that a non-`null` `reference` to an instance of `java.lang.invoke.CallSite` is bound to this dynamic call site. Therefore, the operand stack item which represents a `reference` to the target of the call site object is never `null`. Similarly, it implies that the method descriptor in the call site specifier is semantically equal to the type descriptor of the *method handle to be invoked* as if by execution of an *invokevirtual* instruction. Together, these invariants mean that an *invokedynamic* instruction which is bound to a call site object never throws a `NullPointerException` or a `java.lang.invoke.WrongMethodTypeException`.
