Quick revision
The short answer
The refactoring changed the objects, not just the syntax. A lambda's class has a generated name and its toString is meaningless, a non-capturing lambda may be the same instance every time, so identity checks and sets behave differently, and this or an unqualified toString() inside the body now refers to the outer class. A reviewer should look for code that depends on identity, class names, toString or this, refuse conversions of anonymous classes that keep state, and run the full test suite.
What to remember
In this answer, we saw that converting anonymous classes to lambdas changes the objects behind the callbacks. Lambda classes have generated names and no useful toString, non-capturing lambdas may be one shared instance so identity and set membership change, this and unqualified Object methods now refer to the outer class, and anonymous classes with state cannot be converted without redesign. Reviewers should search for dependencies on identity, class names, toString and this, keep the change in its own commit, and run every test.
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.
The refactoring changed the objects, not just the syntax. A lambda's class has a generated name and its toString is meaningless, a non-capturing lambda may be the same instance every time, so identity checks and sets behave differently, and this or an unqualified toString() inside the body now refers to the outer class. A reviewer should look for code that depends on identity, class names, toString or this, refuse conversions of anonymous classes that keep state, and run the full test suite.
Why this question is asked
Converting anonymous classes to lambdas is one of the safest-looking refactorings there is: the IDE does it, the code gets shorter, and almost every test stays green. That is why the one failing test is valuable. It shows that a lambda is not an anonymous class with less typing; it is a different kind of object with different rules for this, naming and identity. Interviewers ask this scenario to see whether we can explain a subtle failure from first principles and turn it into review guidance. In this answer, we will recap what the IDE actually changed, reproduce the toString and identity differences, look at the change in this and at stateful classes, and finish with what a reviewer should check.
1What the IDE Actually Changed
Before the refactoring, every callback was an anonymous inner class: a real class compiled to a file such as OrderService$1.class, and every time the expression ran, a new object of that class was created. Inside its body, this was the callback object.
After the refactoring, each callback is a lambda. Its body is compiled to a private method in OrderService, and the object that implements the interface is produced at runtime by the JVM through an invokedynamic instruction. The JVM is free to decide what that object looks like, what its class is called and whether it creates a new one each time. Those freedoms are exactly where our test broke.
2ToString and Class Names Are No Longer Meaningful
A typical test or log line prints a callback to identify it. With anonymous classes, the default toString showed the class name, which a test might match on.
Notice that the lambda's class name is generated at runtime and contains a hidden-class suffix that changes between JDK versions and even between runs. Any assertion on the class name, on toString, or on isAnonymousClass(), and any code that uses the class name as a key, for example in metrics or a registry, now sees something different. The fix is never to rely on these: if a callback needs a readable name, it should be a named class or carry a description explicitly.
3Identity: One Instance Where There Used to Be Many
The more surprising change is identity. An anonymous class expression always creates a new object. A lambda that captures nothing, no local variables and not this, is in practice created once per call site and the same instance is returned every time. The specification deliberately allows this and does not promise either way.
7 messages in order. Play them, or step through them one at a time.
With an anonymous class the second evaluation returned a new object and the set held two.
So a test that registers the same callback twice and expects two listeners, or a registry that deduplicates by identity, now behaves differently. This is a plausible cause of our failing test: the callback became a shared singleton, and a set or an identity map collapsed two registrations into one. The reverse is also possible: a capturing lambda creates a new instance each time, so code that tried to unregister a listener by passing a freshly written lambda never finds the registered one.
4This and Unqualified Calls Now Mean the Outer Class
Inside an anonymous class, this, toString(), hashCode() and equals() all referred to the callback object. Inside a lambda they refer to the enclosing instance, because a lambda body has the same meaning as the code around it. A conversion that looks mechanical can therefore change behaviour.
Good IDEs warn about or skip conversions that use this explicitly, but unqualified calls such as toString() inside the body are easy to miss. The self-unregistering listener above cannot be a plain lambda at all; it needs a variable that holds the listener, or it should stay an anonymous class.
5Stateful Anonymous Classes Cannot Simply Become Lambdas
Some anonymous classes keep state in their own fields, such as a counter or a cached value. A lambda has no fields, so a conversion would have to move that state into a captured object, for example an AtomicInteger declared outside, which changes who shares it. If the expression runs once per request, an anonymous class gives each request its own counter, while a single captured AtomicInteger outside a loop would be shared. These classes need a redesign, usually into a small named class, not a mechanical conversion.
6What a Reviewer Should Look For
For a large conversion like this, the reviewer cannot read every line with equal care, so they look for the patterns that change meaning. The table below is the checklist we would attach to such a pull request, and the last step is always the full test suite, including integration tests, not only the module that was touched.
| Look for | Why it matters |
|---|---|
this, toString(), hashCode() in the body | they now refer to the outer object |
Identity checks, IdentityHashMap, sets of callbacks | non-capturing lambdas may be one shared instance |
| Unregister or remove by passing the callback | a new lambda is not the registered one |
getClass(), class names, toString in tests or logs | lambda class names are generated and unstable |
| Fields in the anonymous class | state cannot move into a lambda without redesign |
| Serializable callbacks | serialized form and compatibility change |
We would also ask for the conversion to be its own commit or pull request, with no behaviour changes mixed in, so that if a problem shows up later, it can be traced to this change and reverted cleanly.
7Follow-up questions
Is it a bug in the JDK that two lambdas from the same call site are the same object?
No. The Java Language Specification says that evaluating a lambda expression may produce a new object or an existing one, so code must not depend on either. The current implementation reuses one instance for non-capturing lambdas because that saves allocation, but that is an implementation choice that could change.
How should the failing test be fixed?
First decide what behaviour is intended. If registering the same callback twice should give two listeners, the registry should not deduplicate by identity, or the test should register two distinct named instances. If the test only checked the class name or toString, rewrite it to check behaviour instead, such as whether the callback ran.
Can tools make this refactoring safer?
Yes. IntelliJ IDEA and OpenRewrite skip conversions where this is used explicitly or where the class has fields. They cannot see identity assumptions in other parts of the code, though, which is why the review checklist and the full test run are still needed.
8Conclusion
In this answer, we saw that converting anonymous classes to lambdas changes the objects behind the callbacks. Lambda classes have generated names and no useful toString, non-capturing lambdas may be one shared instance so identity and set membership change, this and unqualified Object methods now refer to the outer class, and anonymous classes with state cannot be converted without redesign. Reviewers should search for dependencies on identity, class names, toString and this, keep the change in its own commit, and run every test.
Open with
The IDE changed more than syntax. An anonymous class is a real class, and every evaluation creates a new object whose toString shows a name like OrderService dollar 1.
Then, if they want more
A lambda's class is generated at runtime, so its name contains something like dollar dollar Lambda with a hidden-class suffix that is not stable, and its toString is meaningless.
Identity changes too: a lambda that captures nothing is in practice one shared instance per call site, so registering it twice into a set gives one entry where the anonymous class gave two, and unregistering with a freshly written lambda never matches. I suspect our test hit one of those, a toString or class name assertion, or an identity-based registry.
There is also a semantic change: inside a lambda, this and unqualified calls like toString refer to the outer class, so a listener that logged itself or unregistered itself now does it with the service object. And anonymous classes that kept state in fields cannot become lambdas without redesign.
As a reviewer I would search the diff for this, toString, getClass, identity comparisons, IdentityHashMap, sets of callbacks and unregister calls, reject conversions of stateful classes, insist the conversion is its own commit with no other changes, and run the complete test suite, including integration tests, before merging.
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 "Your team converted hundreds of anonymous classes to lambdas with an IDE refactoring....": https://scrivio-interview-library.netlify.app/java/lambdas/790a13467e/