Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

MethodHandles — typed handles for methods and fields

MethodHandles provides factories and transformations for handles. A Lookup object controls access when handles are created; findVirtual creates an instance-method handle, while findVarHandle provides access to an instance field.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodHandle method = lookup.findVirtual(Owner.class, "method", MethodType.methodType(ReturnType.class, ParameterType.class));
VarHandle field = lookup.findVarHandle(Owner.class, "field", FieldType.class);

java.lang.invoke.MethodHandles contains static factories and handle transformations. A MethodHandle is a typed, executable reference to an operation; a VarHandle represents a variable and its access modes. These are distinct abstractions, not interchangeable names.

MethodHandles.lookup() creates a MethodHandles.Lookup with the calling class's privileges, including access to its private members. It does not grant arbitrary access to other classes. Access is checked when a handle is created; sharing a privileged handle transfers the ability to perform its operation.

MethodHandles.Lookup.findVirtual takes a class, name, and MethodType without the receiver; the resulting handle has the receiver as its first parameter. Non-private methods use dynamic dispatch. invokeExact requires exact argument and return types; a mismatch causes WrongMethodTypeException. This is why the example includes the (int) cast.

MethodHandles.Lookup.findVarHandle takes a class, name, and exact instance-field type. The object is the coordinate for get and set. These plain modes do not provide volatile semantics; final fields do not support writes. Lookup can throw NoSuchMethodException, NoSuchFieldException, or IllegalAccessException, depending on the operation and cause.

Examples

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.invoke.VarHandle;

public class MethodHandlesDemo {
    private int value = 3;

    private int add(int amount) {
        return value + amount;
    }

    public static void main(String[] args) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle add = lookup.findVirtual(
                MethodHandlesDemo.class, "add",
                MethodType.methodType(int.class, int.class));
        VarHandle value = lookup.findVarHandle(
                MethodHandlesDemo.class, "value", int.class);

        MethodHandlesDemo target = new MethodHandlesDemo();
        value.set(target, 7);
        int result = (int) add.invokeExact(target, 5);
        System.out.println(result);
        System.out.println((int) value.get(target));
    }
}

Result

12
7

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources