Quick revision
The short answer
invokedynamic was added in Java 7, through JSR 292, so that dynamic languages on the JVM such as JRuby and Groovy could define their own call linkage instead of emulating it with reflection. Java itself then used it for lambdas, string concatenation, records' object methods and pattern-matching switch. Because a bootstrap method in the JDK decides the implementation at run time, the platform can improve these features without changing class files already compiled, which moved strategy from javac into the runtime library and made language evolution cheaper. Constant dynamic later extended the same idea to constants.
What to remember
In this answer, we saw that invokedynamic was added in Java 7 so dynamic languages could define their own call linkage with bootstrap methods and relinkable call sites, which the JIT can optimise. Java itself then built lambdas, string concatenation, records' object methods and pattern switch on it. Bootstrap-based linkage moved translation strategy from javac into the runtime library, so the JDK can improve features without recompiling existing class files, and constant dynamic extended the same idea to constants.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
invokedynamic was added in Java 7, through JSR 292, so that dynamic languages on the JVM such as JRuby and Groovy could define their own call linkage instead of emulating it with reflection. Java itself then used it for lambdas, string concatenation, records' object methods and pattern-matching switch. Because a bootstrap method in the JDK decides the implementation at run time, the platform can improve these features without changing class files already compiled, which moved strategy from javac into the runtime library and made language evolution cheaper. Constant dynamic later extended the same idea to constants.
Background to this question
This question asks us to look at the platform from a distance: a single bytecode instruction, added for other languages, quietly became the mechanism behind most new Java language features of the last decade. Understanding why tells us how the JDK team thinks about compatibility and evolution, and that thinking is useful when we design long-lived platforms ourselves. In this answer, we will recap the problem dynamic languages had before Java 7, see what invokedynamic gave them, follow how Java adopted it feature by feature, and draw out the design lesson about where strategy should live.
1A Quick Recap: The Problem Before Java 7
The JVM was designed for Java, and its call instructions reflect Java's rules. invokevirtual, invokeinterface, invokestatic and invokespecial each name an exact owner class, method name and type, resolved once and fixed forever. That is perfect for a statically typed language, where the compiler knows which method a call refers to.
Languages such as Ruby, Python, JavaScript and Groovy do not know that at compile time. In Ruby, a.plus(b) may call different methods depending on the run-time class of a, and methods can even be added to a class while the program runs. JVM implementations of these languages had to compile every call into a call to their own runtime, which then looked up the method, often with reflection, on every execution. That indirection hid the real target from the JIT, so it could not inline the call, and dynamic languages on the JVM ran far slower than they could have.
2What invokedynamic Gave Dynamic Languages
JSR 292, delivered in Java 7, added two things: method handles, typed references to executable code that the JIT understands, and the invokedynamic instruction. An invokedynamic call site names a bootstrap method supplied by the language runtime. On first execution the JVM calls it, and it returns a CallSite whose target is a method handle chosen by the language's own rules. The JVM then calls that target directly and can inline it.
For dynamic languages the key was that call sites can be relinked. A runtime can return a MutableCallSite whose target is a guard, a test such as 'is the receiver still a String?', with a fast path for the type seen so far and a fallback that relinks when the type changes. That is an inline cache, the classic technique of dynamic language virtual machines, now built from JVM parts the JIT can optimise. JRuby and Nashorn, the JavaScript engine shipped with Java 8, were early heavy users.
3How Java Itself Adopted It
The surprise came in Java 8. Lambdas needed a way to turn a block of code into an object implementing a functional interface. Instead of generating an inner class file for every lambda, which would have fixed the strategy in every compiled jar forever, javac emits an invokedynamic whose bootstrap is LambdaMetafactory, and the JDK decides at run time how to produce the object. After that, the pattern repeated for almost every feature that needed a non-trivial translation.
| Release | Feature | Bootstrap in the JDK |
|---|---|---|
| Java 8 | Lambdas and method references | LambdaMetafactory |
| Java 9 | String concatenation (JEP 280) | StringConcatFactory |
| Java 16 | Records: toString, equals, hashCode | ObjectMethods |
| Java 21 | Pattern matching for switch | SwitchBootstraps (typeSwitch, enumSwitch) |
Each row follows the same recipe: javac writes a small invokedynamic with the essential facts as constant arguments, and a bootstrap class in the JDK builds the implementation on first use.
4Why Moving Strategy Into the Runtime Matters
Before invokedynamic, javac was the only place a translation strategy could live. Whatever bytecode it wrote was baked into every class file and every published jar. If the JDK team later found a faster way to concatenate strings, only code recompiled with a new javac would benefit, and libraries compiled years ago would carry the old strategy forever. Worse, the old strategy became a compatibility constraint, since the JVM had to keep running it well.
With invokedynamic, the class file records only intent: concatenate these values with this recipe, create a lambda for this method, compute equals over these record fields. The strategy is code in the JDK's runtime library, and it can change in any release. String concatenation is the clearest example: its internal strategy has been reworked more than once since Java 9, and every class compiled for Java 9 or later picked up those changes without recompiling. Lambdas moved to hidden classes in JDK 15 the same way.
So the cost of evolving the language went down. New features need only a small javac change plus a library class that can be tuned, fixed and optimised later, rather than a permanent decision about bytecode shapes on day one.
- Source codelambda, +, record, switch on patterns
- javacemits invokedynamic with constant facts only
- Class filerecords intent, stable for years
- JDK bootstrap classeschoose the strategy, change per release
- JITinlines the linked method handle
5 layers, from the top down. Step through them one at a time.
5Constant Dynamic: The Same Idea for Constants
Java 11 extended the idea with JEP 309, constant dynamic, often called condy. A CONSTANT_Dynamic entry in a class's constant pool is resolved by calling a bootstrap method the first time the constant is used, and the result is then treated as a true constant. That lets compilers and libraries define lazily computed constants of any type, such as a complex descriptor or a pre-built object, without a static initializer that runs eagerly. It applies the same principle to data that invokedynamic applied to calls: the class file states what is needed, and the runtime decides how to produce it, once.
The lesson for our own platforms is general. When we design a contract that many independent artifacts will be compiled against, such as an SDK, a plugin API or a wire format, we should put the stable intent in the contract and keep the strategy behind it in code we can upgrade, so improvements reach old clients without asking them to rebuild.
6Follow-up questions
Did invokedynamic make Java slower to start?
Slightly, in one way: each call site runs its bootstrap method the first time, which costs time during warm-up, and lambda-heavy applications can have thousands of them. The JDK reduces this with class data sharing archives that can store pre-generated lambda classes, and Project Leyden works on saving more linkage ahead of time. After linking, call sites are as fast as ordinary calls.
Why did Java not just generate inner classes for lambdas, like for anonymous classes?
It would have worked, but it would have fixed the translation in every compiled class file, added one class file per lambda to every jar, and made it impossible to change strategy later. Using invokedynamic kept the class file small and left the JDK free to change how lambda objects are created, which it did when it moved them to hidden classes.
Is Nashorn still in the JDK?
No. Nashorn was deprecated in Java 11 and removed in Java 15; it is now a separate project. GraalVM's JavaScript engine is a common replacement. invokedynamic itself remains central, because the Java language now depends on it.
7Conclusion
In this answer, we saw that invokedynamic was added in Java 7 so dynamic languages could define their own call linkage with bootstrap methods and relinkable call sites, which the JIT can optimise. Java itself then built lambdas, string concatenation, records' object methods and pattern switch on it. Bootstrap-based linkage moved translation strategy from javac into the runtime library, so the JDK can improve features without recompiling existing class files, and constant dynamic extended the same idea to constants.
Open with
invokedynamic came from JSR 292 in Java 7, and its original purpose was dynamic languages. Languages like JRuby or Groovy don't know at compile time which method a call refers to, so before Java 7 they routed every call through their own runtime, often using reflection, and the JIT couldn't inline anything. invokedynamic lets a language define linkage itself: the call site names a bootstrap method, which on first execution returns a CallSite targeting a method handle.
Then, if they want more
Call sites can even be relinked, so runtimes can build inline caches the JIT understands. What's interesting is how Java itself adopted it. Lambdas in Java 8 use LambdaMetafactory instead of inner classes. String concatenation since Java 9 uses StringConcatFactory. Records' toString, equals and hashCode use ObjectMethods, and pattern matching switch uses SwitchBootstraps.
In each case javac only records the intent, and a bootstrap class in the JDK chooses the implementation at run time. That moved strategy from the compiler into the runtime library: the JDK can improve concatenation or lambda creation in a new release and old class files benefit without recompiling. That made language evolution much cheaper.
Constant dynamic in Java 11 applies the same idea to constants resolved lazily by a bootstrap. For my own platforms, the lesson is to keep intent in the contract and strategy in upgradable code.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 7, Java 11, Java 15, Java 8
- 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.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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 "Why did the JVM add invokedynamic in Java 7, and how has it shaped the evolution of the...": https://scrivio-interview-library.netlify.app/java/reflection-and-method-handles/d46d535bd5/