Scrivio Interview library
InternalsIntermediate7 min read · updated 12 November 2026

What is a MethodHandle and how does it differ from java.lang.reflect.Method? Explain Lookup, MethodType and when access is checked.

A MethodHandle is a typed, directly executable reference to a method, constructor or field accessor. It is created by a MethodHandles.Lookup, which checks access once, at lookup time, using the rights of its lookup class, while java.lang.reflect.Method describes a member and checks access on each invoke. A MethodType gives the exact parameter and return types: invokeExact needs an exact match, invoke adapts types. Handles held in static final fields can be inlined by the JIT, and adapters such as bindTo, insertArguments, filterReturnValue and guardWithTest combine them into new behaviour.

Why this question is asked

Reflection and method handles both call code chosen at run time, so they are easy to confuse, but they were designed for different jobs. Method describes a class's members for tools and frameworks; MethodHandle is a building block for executing calls fast, which is why the JDK uses it for lambdas, string concatenation and, since JDK 18, inside reflection itself. Interviewers ask this to see whether we understand the access model and the type rules, which are where most surprises come from. In this answer, we will recap what a Method is, see what makes a handle different, look at Lookup and when access is checked, study MethodType and the two invoke methods, and combine handles with adapters.

1A Quick Recap of java.lang.reflect.Method

A Method object describes one method of a class: its name, parameter types, return type, modifiers and annotations. It is primarily a description, and calling it is one of the things it can do. Method.invoke(target, args) takes the arguments as an Object array, boxes primitives, checks that the caller is allowed to call the method unless setAccessible(true) was used, and wraps any exception from the method in InvocationTargetException. That flexibility makes it easy to use and convenient for frameworks that inspect classes, but every call pays for the checks and the boxing.

2What a MethodHandle Is

A MethodHandle, from java.lang.invoke, is not a description. It is a typed reference that can be executed directly, closer to a function pointer than to a Method. It can point to a method, a constructor, or a field getter or setter, and it has a fixed type. Calling it passes arguments with their real types, and the target's exceptions come out unchanged. Handles are also composable: we can build new handles from existing ones, which is how the JDK assembles lambdas and string concatenation.

Method and MethodHandle side by side
PropertyMethodMethodHandle
PurposeDescribe a member, optionally call itExecute a call
Access checkOn each invoke, unless setAccessibleOnce, when the Lookup creates it
ArgumentsObject array, primitives boxedTyped, as in the MethodType
ExceptionsWrapped in InvocationTargetExceptionThrown directly
CompositionNonebindTo, insertArguments, filters, guards
MetadataName, annotations, modifiersOnly its type

3Lookup and When Access Is Checked

Handles are created by a MethodHandles.Lookup. Calling MethodHandles.lookup() returns a Lookup that carries the access rights of the class that called it, the lookup class. When we ask it for a handle with findVirtual, findStatic, findConstructor, findGetter or a similar method, it checks right then whether the lookup class could access that member in normal Java code. If not, it throws IllegalAccessException. If yes, the handle is created, and no further access check happens when it is called.

Java
class Vault {
private String secret() { return "42"; }
static MethodHandles.Lookup lookup() { return MethodHandles.lookup(); } // Vault's own rights
}
MethodType t = MethodType.methodType(String.class);
MethodHandles.lookup().findVirtual(Vault.class, "secret", t);
// IllegalAccessException: our class may not access private Vault.secret() (wording varies by version)
MethodHandle h = Vault.lookup().findVirtual(Vault.class, "secret", t); // checked once, allowed
System.out.println((String) h.invokeExact(new Vault())); // 42, no check on the call

This is a capability model: a handle is like a key. Whoever holds it can call the method, even code that could not have looked it up itself, so Vault can choose to hand a handle to a trusted helper. For frameworks there are two related tools. MethodHandles.publicLookup() can only reach public members of exported packages. MethodHandles.privateLookupIn(Target.class, lookup) gives private access to another class, but only if that class's module opens its package to the caller, the same rule that strong encapsulation applies to setAccessible.

4MethodType, invokeExact and invoke

A MethodType describes a handle's exact signature: the return type and the parameter types, with primitives kept as primitives. For an instance method the receiver is the first parameter, so a handle to String.length() has type (String)int.

There are two ways to call a handle. invokeExact requires the call site's types, as the compiler sees them from the static argument types and the cast on the result, to match the handle's type exactly, with no conversions. invoke allows the usual conversions, such as casting, boxing, unboxing and widening, by adapting the handle with asType() before the call.

Java
MethodHandle abs = MethodHandles.lookup().findStatic(Math.class, "abs",
MethodType.methodType(int.class, int.class)); // (int)int
int a = (int) abs.invokeExact(-5); // 5: call site (int)int matches
Integer boxed = -5;
int b = (int) abs.invokeExact(boxed); // WrongMethodTypeException: call site is (Integer)int
int c = (int) abs.invoke(boxed); // 5: invoke unboxes the Integer
MethodHandle general = abs.asType(MethodType.methodType(Object.class, Object.class));
Object d = general.invokeExact((Object) (Integer) (-5)); // 5: adapted once, then exact calls

So the strictness is deliberate. An exact call needs no conversion work, which keeps it as cheap as possible; when we want flexibility, we either pay for it on each call with invoke or once with asType.

5Performance and Combining Handles

Because a handle has a fixed type and a fixed target, the JIT can treat a handle stored in a static final field as a constant and inline the target method right into the caller, making the call about as fast as a direct one. A handle in a non-final field or created on each call cannot be inlined that way and loses most of the advantage.

Handles can also be combined into new handles with the static methods of MethodHandles and the instance method bindTo. bindTo fixes the receiver, insertArguments fixes any arguments, filterReturnValue passes the result through another handle, and guardWithTest chooses between two handles based on a test handle, a small if-statement made of handles. Language runtimes build whole inline caches out of these pieces.

Java
var L = MethodHandles.lookup();
MethodHandle concat = L.findVirtual(String.class, "concat",
MethodType.methodType(String.class, String.class)); // (String,String)String
MethodHandle upper = L.findVirtual(String.class, "toUpperCase",
MethodType.methodType(String.class)); // (String)String
MethodHandle isEmpty = L.findVirtual(String.class, "isEmpty",
MethodType.methodType(boolean.class)); // (String)boolean
MethodHandle greet = MethodHandles.insertArguments(concat, 0, "Hello, "); // (String)String
MethodHandle loud = MethodHandles.filterReturnValue(greet, upper); // (String)String
MethodHandle anon = MethodHandles.dropArguments(
MethodHandles.constant(String.class, "Hello, stranger"), 0, String.class); // (String)String
MethodHandle safe = MethodHandles.guardWithTest(isEmpty, anon, loud);
System.out.println((String) greet.invokeExact("Ana")); // Hello, Ana
System.out.println((String) safe.invokeExact("Ana")); // HELLO, ANA
System.out.println((String) safe.invokeExact("")); // Hello, stranger
System.out.println((String) upper.bindTo("abc").invokeExact()); // ABC
Step throughHow the combined handle runs
  1. safe.invokeExact(name)type (String)String
  2. isEmpty(name)?
    Yesanon: return Hello, strangerNoloud: greet then upper
  3. greet inserts "Hello, "concat("Hello, ", name)
  4. filterReturnValue applies upperHELLO, ANA

4 steps in order. Play them, or step through them one at a time.

6Follow-up questions

Why does invokeExact throw WrongMethodTypeException when you pass an Integer for an int parameter?

Because invokeExact compares types, not values, and allows no conversion of any kind. The compiler records the call site's type from the static type of each argument, so passing an Integer makes the call site (Integer)int while the handle is (int)int. Unboxing would be a conversion, so the types do not match and the JVM throws WrongMethodTypeException before calling anything. Passing an int, calling invoke, or adapting the handle with asType fixes it.

When would you still use reflection instead of method handles?

When we need metadata: annotations, parameter names, generic types, modifiers or the list of members. Handles carry only a type. Frameworks typically use reflection to discover what to call, then convert the chosen members into handles with Lookup.unreflect for the hot path.

What does Lookup.unreflect do?

It turns a java.lang.reflect.Method into a MethodHandle, applying the Lookup's access rules at that moment. unreflectGetter and unreflectSetter do the same for fields. It is the bridge between discovery with reflection and execution with handles.

7Conclusion

In this answer, we saw that a Method describes a member and checks access on each invoke, while a MethodHandle is a typed, directly executable reference to a method, constructor or field accessor. A Lookup creates handles and checks access once, based on its lookup class. A MethodType fixes the exact types, so invokeExact needs an exact match and invoke adapts. Handles in static final fields can be inlined by the JIT, and adapters such as bindTo, insertArguments, filterReturnValue and guardWithTest combine them into new behaviour.

What to say in the interviewAbout 80 seconds aloud

Open with

A MethodHandle is a typed, directly executable reference to a method, a constructor, or a field getter or setter. java.lang.reflect.Method is mostly a description of a member, with annotations, modifiers and so on, and invoking it boxes arguments into an Object array, checks access on each call unless setAccessible was used, and wraps exceptions in InvocationTargetException.

Then, if they want more

Handles are created by a MethodHandles.Lookup. lookup() returns one carrying the access rights of the calling class, and the access check happens once, when I call findVirtual, findStatic or findConstructor. After that, whoever holds the handle can call it, so it works like a capability. privateLookupIn gives private access to another class, but only if its module opens the package.

The MethodType is the exact signature, with the receiver as first parameter for instance methods. invokeExact demands that the call site types match exactly, including the cast on the result, so passing an Integer to an int parameter throws WrongMethodTypeException; invoke adapts types with boxing and casts.

For performance I keep handles in static final fields, because the JIT treats them as constants and can inline them. Handles also compose: bindTo fixes the receiver, insertArguments fixes arguments, filterReturnValue post-processes results, and guardWithTest picks between two handles based on a test.

About this answer

Last updated
12 November 2026
Versions it names
Java 18
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is a MethodHandle and how does it differ from java.lang.reflect.Method? Explain...": https://scrivio-interview-library.netlify.app/java/reflection-and-method-handles/4b65de1df9/