Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Rewrite this reflective call using a MethodHandle:

Java
Method m = String.class.getMethod("toUpperCase");
Object r = m.invoke("abc");

Explain what type the handle has.

We look up the method once with MethodHandle mh = MethodHandles.lookup().findVirtual(String.class, "toUpperCase", MethodType.methodType(String.class)) and call it with String r = (String) mh.invokeExact("abc"). The handle's type is (String)String: the receiver becomes the first parameter, followed by the method's parameters, of which there are none, and the return type is String. invokeExact requires the call site to match that type exactly, including the cast, or it throws WrongMethodTypeException.

Why this question is asked

Method handles are the modern, faster way to call code chosen at run time. The JDK itself uses them for lambdas, string concatenation and, since JDK 18, for core reflection, and libraries such as Jackson and serialisation frameworks use them on hot paths. Interviewers ask for this small rewrite because getting the type right is the whole skill: once we understand why the handle is (String)String, every other lookup follows the same rule. In this answer, we will recap the reflective version, rewrite it step by step, explain the handle's type, see what goes wrong with invokeExact, and store the handle for performance.

1A Quick Recap of the Reflective Version

The original code finds a Method object by name and calls it with invoke.

Java
Method m = String.class.getMethod("toUpperCase");
Object r = m.invoke("abc");
System.out.println(r); // ABC

Each invoke() call checks access again, takes its arguments as an Object array, and returns an Object that we must cast. Exceptions thrown by toUpperCase() would arrive wrapped in an InvocationTargetException. A MethodHandle, from java.lang.invoke, is a typed reference to a method that we can call directly: access is checked once when we create it, the arguments are passed with their real types, and the method's own exceptions come out unchanged.

2Rewriting It With a MethodHandle

We need three pieces: a Lookup, which is the object that creates handles and carries the access rights of the class that created it; a MethodType, which describes the return and parameter types; and the right find method for the kind of member.

Java
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodType type = MethodType.methodType(String.class); // returns String, no parameters
MethodHandle mh = lookup.findVirtual(String.class, "toUpperCase", type);
String r = (String) mh.invokeExact("abc");
System.out.println(r); // ABC
System.out.println(mh.type()); // (String)String

findVirtual is the lookup for an instance method, one called on an object, which is dispatched virtually so overriding works. There are matching methods for other members: findStatic for static methods, findConstructor for constructors, findGetter and findSetter for fields, and findSpecial for super calls. The MethodType we pass describes the method as declared, so for toUpperCase() it is only the return type String, with no parameters. The lookup methods throw NoSuchMethodException and IllegalAccessException, and invokeExact is declared to throw Throwable, so real code wraps them accordingly.

3Why the Handle Type Is (String)String

Notice that mh.type() prints (String)String, although the MethodType we passed had no parameters. The reason is that a handle for an instance method needs to be told which object to call it on. findVirtual adds the receiver, the object the method is called on, as an extra first parameter. So the handle behaves like a static function that takes a String and returns a String: give it "abc", and it calls "abc".toUpperCase().

Java
MethodHandle concat = lookup.findVirtual(String.class, "concat",
MethodType.methodType(String.class, String.class));
System.out.println(concat.type()); // (String,String)String
System.out.println((String) concat.invokeExact("ab", "cd")); // abcd
MethodHandle valueOf = lookup.findStatic(String.class, "valueOf",
MethodType.methodType(String.class, int.class));
System.out.println(valueOf.type()); // (int)String: static, so no receiver
From the method to the handle type
MethodLookupHandle type
String toUpperCase()findVirtual(String)String
String concat(String)findVirtual(String,String)String
static String valueOf(int)findStatic(int)String
new StringBuilder(String)findConstructor(String)StringBuilder

So the rule is: the receiver type comes first for instance methods, then the declared parameters, and the return type last. A static method has no receiver, so its handle type is exactly the declared signature.

4What invokeExact Demands From the Call Site

invokeExact is strict. The compiler records a type for each call based on how we wrote it, the symbolic type descriptor, and the JVM compares it with the handle's type. It must match exactly, with no conversions at all. The cast (String) is not a cast in the usual sense; it tells the compiler the call returns String, so the call site's type becomes (String)String.

Java
String ok = (String) mh.invokeExact("abc"); // call site (String)String: matches
Object a = mh.invokeExact("abc"); // call site (String)Object: WrongMethodTypeException
mh.invokeExact("abc"); // call site (String)void: WrongMethodTypeException
Object in = "abc";
String b = (String) mh.invokeExact(in); // call site (Object)String: WrongMethodTypeException
Object c = mh.invoke("abc"); // invoke adapts the types: ABC
Object d = mh.invoke(in); // invoke casts the argument: ABC

So with invokeExact, the declared types of the arguments and the cast on the result must both match the handle. If we cannot guarantee that, invoke() is the forgiving alternative: it adapts the call with the usual casts, boxing and widening, as if by asType(), at a small cost. Most framework code converts handles once with asType() to a general type and then calls invokeExact.

5Keeping the Handle in a static final Field

Creating a handle involves a lookup and access checks, so we do it once. The best place for a frequently used handle is a static final field. The JIT compiler treats static final fields as constants, so it can see exactly which method the handle points to and inline the call, making it as fast as calling toUpperCase() directly.

Java
final class Upper {
private static final MethodHandle TO_UPPER;
static {
try {
TO_UPPER = MethodHandles.lookup().findVirtual(String.class, "toUpperCase",
MethodType.methodType(String.class));
} catch (ReflectiveOperationException e) {
throw new ExceptionInInitializerError(e);
}
}
static String upper(String s) {
try {
return (String) TO_UPPER.invokeExact(s);
} catch (Throwable t) {
throw new IllegalStateException(t);
}
}
}
System.out.println(Upper.upper("abc")); // ABC

A handle stored in an ordinary instance field, or looked up on each call, still works, but the JIT cannot treat it as a constant, and much of the speed advantage over reflection disappears.

6Follow-up questions

Why does findVirtual take a MethodType without the receiver?

Because the MethodType describes the method as it is declared in the class, which is how lookup finds it, just like getMethod("toUpperCase") takes no receiver. The receiver is added to the handle's type afterwards, because the handle needs it as an argument when we call it.

Can bindTo remove the receiver parameter?

Yes. mh.bindTo("abc") returns a new handle of type ()String that always calls toUpperCase() on "abc", so (String) bound.invokeExact() returns ABC. It is the handle version of capturing a variable in a lambda.

Is a MethodHandle always faster than reflection?

Only when the JIT can treat it as a constant, typically in a static final field. A handle created per call or kept in a non-final field is not inlined and may perform about the same as Method.invoke, especially since JDK 18 implements core reflection with method handles internally.

7Conclusion

In this answer, we rewrote a reflective call as MethodHandles.lookup().findVirtual(String.class, "toUpperCase", MethodType.methodType(String.class)) followed by (String) mh.invokeExact("abc"). We saw that the handle's type is (String)String because findVirtual adds the receiver as the first parameter, that invokeExact requires the call site, including the cast, to match exactly or it throws WrongMethodTypeException while invoke() adapts types, and that frequently used handles belong in static final fields so the JIT can inline them.

What to say in the interviewAbout 80 seconds aloud

Open with

I'd write MethodHandle mh = MethodHandles.lookup().findVirtual(String.class, "toUpperCase", MethodType.methodType(String.class)), and then call it as String r = (String) mh.invokeExact("abc"). The lookup object creates handles and checks access once, at lookup time, using the access rights of the class that called lookup(). findVirtual is for instance methods, and the MethodType I pass describes the method as declared: returns String, takes nothing.

Then, if they want more

But the handle's own type is (String)String, because for an instance method the receiver becomes the first parameter, so the handle behaves like a function from String to String. invokeExact is strict: the types at the call site, including the cast on the result and the static types of the arguments, have to match the handle's type exactly.

If I wrote Object r = mh.invokeExact("abc"), the call site would be (String)Object and I'd get WrongMethodTypeException. invoke is the forgiving version, it adapts with casts and boxing. Also, invokeExact throws Throwable, and the target's exceptions come out directly rather than wrapped in InvocationTargetException.

For performance I'd store the handle in a static final field, created in a static initializer, because then the JIT treats it as a constant and can inline the call, making it close to a direct call.

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 "Rewrite this reflective call using a MethodHandle:": https://scrivio-interview-library.netlify.app/java/reflection-and-method-handles/e279853963/