Quick revision
The short answer
A lambda is a function body that adopts the enclosing scope: this means the enclosing instance, and it cannot redeclare an enclosing local variable. An anonymous inner class is a real class: this means the anonymous object, it can shadow names, implement several methods, extend a class and keep state in fields. Lambdas capture the enclosing instance only when they use it, are created through invokedynamic instead of a separate class file, and should not be serialized; anonymous classes have historically always held the outer instance.
What to remember
In this answer, we compared lambdas and anonymous inner classes. A lambda is lexically scoped, so this means the outer instance and it cannot redeclare outer locals, while an anonymous class has its own this and its own scope. Anonymous classes can extend classes, implement several methods and hold state; lambdas cannot. Lambdas capture only what they use, avoid a class file per expression by using invokedynamic, and may reuse one instance when they capture nothing. Neither should be serialized in normal code.
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.
A lambda is a function body that adopts the enclosing scope: this means the enclosing instance, and it cannot redeclare an enclosing local variable. An anonymous inner class is a real class: this means the anonymous object, it can shadow names, implement several methods, extend a class and keep state in fields. Lambdas capture the enclosing instance only when they use it, are created through invokedynamic instead of a separate class file, and should not be serialized; anonymous classes have historically always held the outer instance.
Why this question is asked
Since Java 8 most of us write lambdas where we used to write anonymous classes, and IDEs offer the conversion with one click. Most of the time the two behave the same, which is exactly why the differences surprise people when they matter: a this that suddenly means something else, a name that no longer compiles, or a memory leak that disappears. Interviewers ask this question to see whether we know what a lambda really is rather than treating it as shorter syntax. In this answer, we will recap both forms, then compare them on the meaning of this, scope and shadowing, what each can express, capturing, serialization and performance.
1A Quick Recap of Both Forms
An anonymous inner class is a class without a name, declared and instantiated in one expression. The compiler turns it into a real class file, named something like Outer$1.class, and every evaluation of the expression creates a new object of that class.
A lambda expression is a block of code that the compiler turns into an instance of a functional interface, an interface with a single abstract method. The body of the lambda is compiled into a private method of the enclosing class, and the object that implements the interface is created at runtime. Both forms can read local variables of the enclosing method, but only if those variables are final or effectively final, meaning never assigned after their first value.
2What this Means Inside Each One
The most important difference is the meaning of this. Inside an anonymous class, this refers to the anonymous object itself, because the body is a method of that class. Inside a lambda, this refers to the instance of the enclosing class, exactly as it would in the surrounding code. A lambda is said to be lexically scoped: its body means what the same text would mean just outside it.
Notice that the same is true for unqualified calls such as toString() or hashCode() inside the body. In the anonymous class they call the anonymous object's methods; in the lambda they call the outer object's. To reach the outer instance from an anonymous class, we must write Greeter.this.
3Scope and Shadowing
An anonymous class body is a new class scope, so it may declare a local variable or parameter with the same name as one in the enclosing method; the inner one simply hides the outer one. A lambda does not create a new scope for names. Its parameters and locals live in the same scope as the enclosing method, so reusing a name is a compile error.
The reason for this rule is readability. A lambda is meant to read as part of the surrounding code, so the language refuses to let a short lambda silently hide a variable a few lines above it.
4What Only an Anonymous Class Can Do
A lambda can implement only a functional interface, and only its one abstract method. An anonymous class is a full class: it can implement an interface with several abstract methods, extend an abstract or concrete class and pass constructor arguments to it, override several methods, and keep state in its own fields between calls.
A lambda that needs state has to borrow it from somewhere, such as an AtomicInteger captured from the enclosing method, which is usually a sign that a small named class would be clearer.
5Capturing the Enclosing Instance
An anonymous class declared in an instance method is an inner class, so it has an enclosing instance, and javac has traditionally stored a reference to it in a hidden field called this$0 even when the body never uses it. That is the classic memory leak: a listener registered with a long-lived object keeps the whole outer object, such as an Android activity or a large service, alive. Newer javac versions, from JDK 18, can leave out that field when the anonymous class does not use it and is not serializable, but code compiled for older targets and serializable classes still carry it, so check your compiler.
A lambda captures only what its body uses. A lambda that uses no outer variables and not this captures nothing; one that uses a field captures this, because reading a field means reading this.field. So with lambdas we can see from the body exactly what is held.
6Serialization and Performance
An anonymous class is serializable if its interface or superclass is, but it drags the enclosing instance into the serialized form, which is rarely wanted. A lambda is serializable only if its target type is, for example through a cast to Runnable & Serializable, and the Java documentation strongly discourages it because the serialized form depends on compiler-generated names. In both cases a small named class is the safer choice.
For performance, the difference is mostly in class loading. Each anonymous class is a separate class file that must be loaded and verified, so hundreds of them add start-up time and metaspace. A lambda compiles to an invokedynamic instruction plus a private method in the enclosing class. The first time that instruction runs, LambdaMetafactory creates a hidden class at runtime and links the call site; after that it is as fast as a normal call. A non-capturing lambda typically returns the same instance every time, so it does not even allocate. Once warmed up, the calls themselves cost about the same.
| Aspect | Lambda | Anonymous class |
|---|---|---|
| this | the enclosing instance | the anonymous object |
| New name scope | no: cannot redeclare an outer local | yes: may shadow outer names |
| What it can implement | one functional interface | any interface or class, many methods |
| Own state in fields | no | yes |
| Holds outer instance | only if the body uses it | traditionally always, in instance context |
| Class file at compile time | none, hidden class at runtime | Outer$1.class per expression |
| New object per evaluation | not for non-capturing lambdas | always |
7Follow-up questions
Can a lambda refer to itself, for example to remove itself as a listener?
Not directly, because this means the enclosing object. We either keep the lambda in a field or variable and refer to that, or use an anonymous class, where this is the listener itself. Self-removing listeners are one of the few places where an anonymous class is still the clearer choice.
Why must captured local variables be effectively final in both forms?
Both forms copy the value of a captured local into the object they create, because the method's stack frame may be gone by the time the code runs. If the variable could change later, the copy and the original would disagree, so Java forbids reassigning it and the copy is always correct.
Is a method reference the same as a lambda here?
Yes. A method reference such as this::save is compiled through the same invokedynamic mechanism, and it captures this when it refers to an instance method of the enclosing object, just as a lambda using this would.
8Conclusion
In this answer, we compared lambdas and anonymous inner classes. A lambda is lexically scoped, so this means the outer instance and it cannot redeclare outer locals, while an anonymous class has its own this and its own scope. Anonymous classes can extend classes, implement several methods and hold state; lambdas cannot. Lambdas capture only what they use, avoid a class file per expression by using invokedynamic, and may reuse one instance when they capture nothing. Neither should be serialized in normal code.
Open with
The key difference is that a lambda is part of the surrounding code, while an anonymous class is a real class. Inside a lambda, this means the enclosing instance, and unqualified calls like toString go to the outer object; inside an anonymous class, this is the anonymous object, and we need Outer dot this to reach the outer one.
Then, if they want more
Because a lambda does not introduce a new scope, it cannot redeclare a local variable from the enclosing method, while an anonymous class can shadow it. An anonymous class can also do things a lambda cannot: extend a class like TimerTask, implement an interface with several methods, and keep state in fields.
On capturing, an anonymous class in an instance method has traditionally held a reference to the outer instance whether it uses it or not, which causes listener leaks; newer javac versions can drop it when unused, but a lambda only captures what its body uses.
Serialization works for both only when the type is serializable, and it is discouraged for lambdas because it depends on generated method names.
For performance, each anonymous class is a separate class file to load, whereas a lambda compiles to invokedynamic and a private method, gets its class generated at runtime on first use, and a non-capturing lambda is usually a single shared instance.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 18, 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
- 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 "What are the differences between a lambda and an anonymous inner class? Consider this...": https://scrivio-interview-library.netlify.app/java/lambdas/f8bba09b9c/