Quick revision
The short answer
Yes, but only if its target type is serializable, for example through an intersection cast such as (Runnable & Serializable) () -> ..., and only if everything it captures is serializable too. It is serialized as a SerializedLambda that names the capturing class and a compiler-generated implementation method, so it can only be read back by the same class with compatible synthetic names, which change when the code changes. The Java documentation strongly discourages it; a small named serializable class is the safe alternative.
What to remember
In this answer, we saw that a lambda can be serialized only when its target type is serializable, usually with an intersection cast or an interface that extends Serializable, and only when its captured values are serializable. What is written is a SerializedLambda that names the capturing class and a compiler-generated method, and reading it back requires the same class with the same synthetic names, which change as the code changes. That is why the documentation discourages it, and why a small named serializable class or record is the safe choice.
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.
Yes, but only if its target type is serializable, for example through an intersection cast such as (Runnable & Serializable) () -> ..., and only if everything it captures is serializable too. It is serialized as a SerializedLambda that names the capturing class and a compiler-generated implementation method, so it can only be read back by the same class with compatible synthetic names, which change when the code changes. The Java documentation strongly discourages it; a small named serializable class is the safe alternative.
Why this question is asked
Most developers never serialize a lambda on purpose, but frameworks sometimes do it for us: distributed computing engines ship closures to workers, and some caches and session stores serialize whatever we put in them. When that breaks, the error messages are cryptic, so it helps to know exactly what is written and why it is fragile. Interviewers ask this to test whether we understand that a lambda is compiled into a hidden method of its class. In this answer, we will recap Java serialization, make a lambda serializable, look at what actually goes into the stream, see why reading it back is fragile, and finish with the recommended alternative.
1A Quick Recap of Java Serialization
Java serialization turns an object graph into bytes with ObjectOutputStream and back into objects with ObjectInputStream. A class opts in by implementing the marker interface Serializable. On reading, the JVM finds the class by name, checks that its serialVersionUID matches, and fills in the fields from the stream. So the bytes do not contain code, only the class name and field values; the reader must already have a compatible class.
A lambda is a problem for this model because it has no stable class of its own. Its class is generated at runtime with a name that changes from run to run, so serialization cannot simply write the class name and fields.
2Making a Lambda Serializable
By default a lambda is not serializable, because the functional interfaces it usually targets, such as Runnable or Function, do not extend Serializable. To make one serializable, its target type must be serializable. We can use an interface that extends Serializable, or an intersection cast that combines the functional interface with Serializable.
Notice that the cast does not convert anything at runtime. It changes the target type the compiler uses, and when the target is serializable, javac generates the extra support code needed to write and read the lambda.
3What Actually Gets Written
When a serializable lambda is written, its generated class supplies a writeReplace method that substitutes a java.lang.invoke.SerializedLambda object. That object is what really goes into the stream. It records the capturing class (the class whose code contains the lambda), the functional interface and its method, the implementation method, which is the private synthetic method holding the lambda body with a name such as lambda$main$0 and its signature, and the captured arguments.
On reading, SerializedLambda has a readResolve method that calls a hidden static method named $deserializeLambda$ in the capturing class. javac generates that method; it compares the recorded names and signatures with the lambdas it knows about and recreates the matching one.
6 messages in order. Play them, or step through them one at a time.
So the bytes contain a reference to a method by name, not the code. That single fact explains everything that can go wrong.
4Why Reading It Back Is Fragile
The implementation method name is invented by the compiler. With javac it is built from the enclosing method name and a counter, and for serializable lambdas recent javac versions also mix in a hash; other compilers, such as the Eclipse compiler, use their own scheme. Adding, removing or reordering lambdas in a class, or recompiling with a different compiler, can change the names. If the reader's version of the capturing class does not contain a lambda with the recorded name and signature, $deserializeLambda$ fails with an error such as Invalid lambda deserialization, wrapped in an InvalidObjectException (the exact wrapping depends on the JDK version).
So a serialized lambda is tied to the exact build of its capturing class. That is acceptable only when the writer and reader run identical code, and even then a later code change makes all previously stored bytes unreadable. There is no serialVersionUID to manage compatibility, because the generated names are the contract.
5Captured Values Must Be Serializable Too
The captured arguments are written along with the lambda, so each one must be serializable. A lambda that uses a field captures this, the whole enclosing object, which is a common surprise.
The fix in the second method is to capture only the values the lambda needs, here a String, instead of reaching through this.
6The Safe Alternative: A Named Class
The Javadoc of the java.lang.invoke package and the Java Language Specification both warn that serializing lambdas is strongly discouraged, much like serializing inner classes. The safer design is a small named class, often a record, that implements the interface and Serializable.
Now the stream contains a stable class name, a serialVersionUID we control and an explicit field, so other code changes in the project do not break stored data.
7Follow-up questions
Are method references serializable in the same way?
Yes. A method reference such as String::length with a serializable target type is serialized as a SerializedLambda too. Its implementation method is the referenced method itself, so its name is more stable than a lambda body's synthetic name, but the capturing class still needs a matching $deserializeLambda$ entry.
Why does Apache Spark manage to ship lambdas to workers?
Spark serializes closures, including lambdas, with Java serialization, and it works because the driver and the executors run the same application jar for the job. That is exactly the identical-build condition. It also explains why Spark users often see NotSerializableException when a lambda captures a non-serializable enclosing object.
Does the intersection cast cost anything at runtime?
Very little. javac generates the $deserializeLambda$ method in the capturing class and asks the lambda factory for a serializable class with a writeReplace method. Calling the lambda is as fast as any other lambda.
8Conclusion
In this answer, we saw that a lambda can be serialized only when its target type is serializable, usually with an intersection cast or an interface that extends Serializable, and only when its captured values are serializable. What is written is a SerializedLambda that names the capturing class and a compiler-generated method, and reading it back requires the same class with the same synthetic names, which change as the code changes. That is why the documentation discourages it, and why a small named serializable class or record is the safe choice.
Open with
Yes, a lambda can be serialized, but only if its target type is serializable, for example with an intersection cast like Runnable and Serializable, or a functional interface that extends Serializable.
Then, if they want more
Everything it captures must also be serializable, and a lambda that uses a field captures this, the whole enclosing object, which often causes NotSerializableException. What gets written is not the lambda's class, because that class is generated at runtime.
Instead the lambda's writeReplace substitutes a SerializedLambda that records the capturing class, the functional interface, the name and signature of the synthetic method that holds the body, something like lambda dollar main dollar zero, and the captured arguments. On reading, readResolve calls a generated dollar deserializeLambda dollar method in the capturing class, which looks for a matching lambda.
Because those method names are generated by the compiler from the position and number of lambdas in the class, and differ between compilers, any code change or rebuild can make old bytes unreadable, and there is no serialVersionUID to manage that. That is why the Java documentation strongly discourages serializing lambdas.
If I need a serializable function, I write a small named class or record with a serialVersionUID, so the serialized form has a stable class name and explicit fields.
About this answer
- Last updated
- 12 November 2026
- 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
- java.util.stream, package documentation, java.util.function, package 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 "Can a lambda be serialized? What is required, and why is it discouraged?": https://scrivio-interview-library.netlify.app/java/lambdas/4cf2eb5ff5/