In Java, resolveConstantDesc(lookup) turns a constant description back into the value it describes. For a String, the description and the value are identical, so the method simply returns the same string.
It implements the ConstantDesc interface from java.lang.constant. The MethodHandles.Lookup argument tells other descriptor types which classes they may access while resolving; String ignores it. You rarely need this method outside class-file tools and frameworks.
resolveConstantDesc() is an instance method of java.lang.String, added in Java 12. Call it on a string, as in text.resolveConstantDesc(...); calling it on a null reference throws NullPointerException. Requires Java 12 or newer.
Syntax
Pass a lookup object, usually MethodHandles.lookup().
java
String value = text.resolveConstantDesc(MethodHandles.lookup());
// import java.lang.invoke.MethodHandles
Call
What it does
"a".resolveConstantDesc(MethodHandles.lookup())
"a".
s.resolveConstantDesc(MethodHandles.publicLookup()) == s
true.
The interface method ConstantDesc.resolveConstantDesc declares throws ReflectiveOperationException, but String's override does not. Calling it on a String variable needs no try/catch; calling it through a ConstantDesc variable does.
Library declarations
java
public String resolveConstantDesc(MethodHandles.Lookup lookup);
Parameters
Parameter
Description
lookup
The access context for resolution; ignored by String.
Return Value
Return Value
Description
String
This string.
Java Examples
Example 1 — Resolve a String with a Lookup
The resolved value is the original object.
java
import java.lang.invoke.MethodHandles;
public class ResolveConstantDescBasic {
public static void main(String[] args) {
String s = "hello";
String resolved = s.resolveConstantDesc(MethodHandles.lookup());
System.out.println(resolved);
System.out.println("same object? " + (resolved == s));
}
}
Output
hello
same object? true
How It Works
A lookup object.MethodHandles.lookup() describes what the calling class may access.
Nothing to resolve. A String is already the value it describes, so the method returns this.
Identity check.resolved == s prints true: no copy was created.
Example 2 — Resolve Through the ConstantDesc Interface
Using the interface type brings in its checked exception.
java
import java.lang.constant.ConstantDesc;
import java.lang.invoke.MethodHandles;
public class ResolveConstantDescInterface {
public static void main(String[] args) throws ReflectiveOperationException {
ConstantDesc desc = "config";
Object value = desc.resolveConstantDesc(MethodHandles.lookup());
System.out.println(value);
System.out.println(value.getClass().getSimpleName());
}
}
Output
config
String
How It Works
String is a ConstantDesc. A string literal can be assigned to a ConstantDesc variable directly.
A checked exception appears. Through the interface the method declares ReflectiveOperationException, so main declares throws.
Dynamic dispatch. The String override runs and returns the string, and its class name prints as String.
Example 3 — Describe and Resolve: A Full Round Trip
Combine describeConstable() with resolveConstantDesc().
java
import java.lang.invoke.MethodHandles;
public class ResolveConstantDescRoundTrip {
public static void main(String[] args) {
String original = "round trip";
String back = original.describeConstable()
.orElseThrow()
.resolveConstantDesc(MethodHandles.publicLookup());
System.out.println(back);
System.out.println(back.equals(original));
}
}