Lesson · Lambdas & Functional Interfaces
How lambdas actually work: invokedynamic, not anonymous classes
14 min readHow Lambdas Actually Work
A lambda compiles to a private synthetic method plus an invokedynamic call site. The object that implements your functional interface is created by the runtime on first use, which explains identity, serialization and this-behavior all at once.
What the compiler writes
A lambda body does not become an anonymous class. The compiler moves the body into a private synthetic method of the enclosing class (named lambda$methodName$N) and records the call site as an invokedynamic instruction.
That instruction is not resolved at compile time. It carries a bootstrap reference to LambdaMetafactory, which the JVM consults the first time the surrounding code runs.
Predicate<String> p = s -> !s.isEmpty(); // roughly what the compiler produces: private static synthetic boolean lambda$main$0(String s) { return !s.isEmpty(); } // and at the call site: an invokedynamic that asks LambdaMetafactory for a Predicate
What the runtime does on first use
The first execution of the call site invokes LambdaMetafactory.metafactory, which generates a class implementing the target interface and links it to the synthetic method. From then on the call site is a plain linked call: further executions skip the bootstrap.
Captured variables travel as arguments. A lambda that uses one local gets a factory that takes that value and bakes it into the generated instance; a non-capturing lambda can be implemented with a single cached instance.
- Compiler: body moved to lambda$main$0; call site becomes invokedynamic.
- First run of the line: JVM calls LambdaMetafactory with the target interface and the synthetic method.
- The factory spins a class (visible as Something$$Lambda/0x...) that forwards the single abstract method to the synthetic body.
- The call site is linked; later runs reuse it directly.
- Captured values are passed through the factory per creation, so each capture combination can get its own instance.
Lambda pipeline · compile time vs runtime
compile time
Predicate<String> p = s -> !s.isEmpty();
one line of source
private static synthetic boolean lambda$main$0(String s)
the body, moved into a method
invokedynamic #7, LambdaMetafactory
the use site, unresolved
runtime (first run)
LambdaMetafactory.metafactory(...)
asked once, on first execution
class Demo$$Lambda/0x00000008000c5040
implements Predicate, forwards to lambda$main$0
getClass().getName()
hex suffix varies per run and per JDK
call site linked
later runs reuse it directly
captured limit = 3
baked in per instance, passed via factory
identity unspecified · toString Object · not serializable · this = enclosing
observable rules to code against
Source: one lambda expression. The compiler will not create a class for it; the next steps show what it does instead.
What you can actually observe
None of this is guaranteed API, but the observable consequences are stable enough to debug with:
Predicate<String> p = s -> !s.isEmpty(); System.out.println(p.getClass().getName()); // e.g. LambdaInternalsDemo$$Lambda/0x00000008000c5040
- getClass() shows a runtime name like something$$Lambda/0x000000080..., with a hex suffix that varies per run.
- Identity is unspecified. Two evaluations of the same lambda expression may or may not return the same object. Never use lambdas as map keys or rely on == between them.
- toString and equals are not overridden. They fall back to Object behavior, so printing a lambda is not useful for debugging logic.
- Not serializable by default. If you serialize, write an explicit class or cast the lambda to an intersection type with Serializable, which is fragile and rarely the right design.
- One-time linkage cost. The first execution of a lambda line pays for class spinning; everything afterwards is cheaper than an anonymous class allocation in practice.
Against anonymous classes, point by point
The mental model to keep is the table below. Anonymous classes are still the right tool when you need fields, multiple methods, or a serializable named type. Lambdas win when the behavior is one method and nothing else.
| Aspect | Anonymous class | Lambda |
|---|---|---|
| Compiled form | A separate class file (Outer$1.class) | Private synthetic method + invokedynamic |
| When the type is created | Compile time, eagerly | Runtime on first use of the call site |
| this inside the body | The anonymous instance itself | The enclosing instance |
| Extra members | Fields, initializers, multiple methods | Exactly the one abstract method |
| Identity | Every new instance is a distinct object | Unspecified; may be cached or repeated |
| Serialization | Works when the class is serializable | Not serializable by default |
Lambdas are not faster because they avoid objects. They are cheaper because Java 8 stopped writing one class file per callback site and let the JVM decide when and how to make the object.
Why the JVM designers chose invokedynamic
The pre-8 approach wrote a class per lambda site into the jar. That bloats builds, fills metadata with one-method classes, and fixes the implementation strategy forever. invokedynamic defers the decision to runtime and keeps it open: the platform has since improved class spinning, hidden classes and caching without any source change.
The cost model follows: compile time is cheap (one method), first execution pays a small linking fee, and steady-state execution is a normal virtual call.
Do not test the implementation details
Names like $$Lambda0x..., cached instances and unlinked versus linked call sites are implementation behavior, not a contract. Write code that treats every lambda as a fresh, identity-less behavior; anything else breaks on the next JDK update.
Interview angles
How does a lambda compile?+
Its body becomes a private synthetic method (lambda$enclosing$N) and the use site becomes an invokedynamic instruction with a LambdaMetafactory bootstrap. No class for the lambda is generated at compile time.
Is a lambda an object?+
Eventually, yes: the runtime creates an object implementing the functional interface when the call site is first executed. At the language level, however, the lambda is a behavior with no identity guarantees, and the object may be cached or recreated per evaluation.
Why can a lambda use this of the enclosing class when an anonymous class cannot?+
An anonymous class is a separate class instance, so its this is itself. A lambda has no separate class in source, and the compiler treats this lexically: it means the same thing inside and outside the lambda.
Why are lambdas not serializable by default?+
Serializing one would bake in the generated runtime class and its linkage, which are not stable across runs or JDK versions. If you must serialize behavior, use a real class (often an enum or a serializable strategy object) instead.
What is the performance story?+
One capture-and-link cost the first time the call site runs, then ordinary invocation. In practice lambdas avoid the class-loading and metadata cost of anonymous classes, which is why they are the default idiom rather than a micro-optimization.