Actually lambda expressions aren't "just an abbreviation to implement anonymous classes". The benefit of using lambda expression is that it has direct access to instance of this class (the class from which it was called), while anonymous class don't (it has its own this instance).

In other words, anonymous classes introduce a new scope. so that names are resolved from their superclasses and interfaces and can shadow names that occur in the lexically enclosing environment. For lambdas, all names are resolved lexically.

https://stackoverflow.com/a/22640484 And also

Lambda performance with Anonymous classes

When application is launched each class file must be loaded and verified.

Anonymous classes are processed by compiler as a new subtype for the given class or interface, so there will be generated a new class file for each.

Lambdas are different at bytecode generation, they are more efficient, used invokedynamic instruction that comes with JDK7.

For Lambdas this instruction is used to delay translate lambda expression in bytecode untill runtime. (instruction will be invoked for the first time only)

As result Lambda expression will becomes a static method(created at runtime). (There is a small difference with stateles and statefull cases, they are resolved via generated method arguments)

https://stackoverflow.com/a/33874965

For instance:

interface Supplier {
   void foo();
}

class A {

   private String name;

   public void doSome() {
      baz(() -> System.out.println(this.name));
   }

   private void baz(Supplier sup){
     sup.foo();
   }
}

or

class A {

   private String name;

   public void doSome() {
      baz(new Supplier() {
         void foo() {
            System.out.println(A.this.name);
         }
      });
   }

   private void baz(Supplier sup){
     sup.foo();
   }
}

I recommend to read this: Java8 Lambdas vs Anonymous classes.

Answer from Andrew Kolesnyk on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › core java › convert anonymous class into lambda in java
Convert Anonymous Class into Lambda in Java | Baeldung
May 29, 2025 - As we can see, we replaced the anonymous class with an arrow between the send() parameter and its body. We can enhance this even more: We can change the lambda statement to a lambda expression by removing the parameter type and the return statement:
🌐
Medium
medium.com › swlh › super-basic-java-anonymous-class-functional-interface-and-lambda-expression-fba411a787
Java Chapter 1 : Anonymous Class, Functional Interface, and Lambda Expression | by Sohee Kim | The Startup | Medium
June 8, 2021 - So basically, you can write the same code using an Anonymous class! Clearly, using Functional Interface and Lambda Expressions add syntactic sugar to your code. It lets you use functions as arguments or return a function as a result without ...
🌐
Baeldung
baeldung.com › home › java › core java › lambda expression vs. anonymous inner class
Lambda Expression vs. Anonymous Inner Class | Baeldung
January 8, 2024 - An anonymous class is an expression assigned to a reference variable of the class or interface it implements. Therefore, we also put a semicolon at the end. On the other hand, a lambda expression is a method without a name.
🌐
Oracle
docs.oracle.com › javase › tutorial › java › javaOO › lambdaexpressions.html
Lambda Expressions (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
In these cases, you're usually trying to pass functionality as an argument to another method, such as what action should be taken when someone clicks a button. Lambda expressions enable you to do this, to treat functionality as method argument, or code as data.
🌐
Blogger
javarevisited.blogspot.com › 2015 › 01 › how-to-use-lambda-expression-in-place-anonymous-class-java8.html
How to use Lambda Expression in Place of Anonymous Class in Java 8 - Example Tutorial
Despite all these goodies and flexibility, the Anonymous class adds too much boilerplate code, making it hard to read and understand. Now with Java 8, you have got the ability to pass a function to another function in the form of the lambda expression, you can easily replace your Anonymous class with lambda expression in Java.
🌐
JetBrains
jetbrains.com › help › inspectopedia › LambdaCanBeReplacedWithAnonymous.html
Lambda can be replaced with anonymous class | Inspectopedia Documentation
June 30, 2026 - Expanding lambda expressions to anonymous classes may be useful if you need to implement other methods inside an anonymous class. ... Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable ...
Top answer
1 of 2
5

Actually lambda expressions aren't "just an abbreviation to implement anonymous classes". The benefit of using lambda expression is that it has direct access to instance of this class (the class from which it was called), while anonymous class don't (it has its own this instance).

In other words, anonymous classes introduce a new scope. so that names are resolved from their superclasses and interfaces and can shadow names that occur in the lexically enclosing environment. For lambdas, all names are resolved lexically.

https://stackoverflow.com/a/22640484 And also

Lambda performance with Anonymous classes

When application is launched each class file must be loaded and verified.

Anonymous classes are processed by compiler as a new subtype for the given class or interface, so there will be generated a new class file for each.

Lambdas are different at bytecode generation, they are more efficient, used invokedynamic instruction that comes with JDK7.

For Lambdas this instruction is used to delay translate lambda expression in bytecode untill runtime. (instruction will be invoked for the first time only)

As result Lambda expression will becomes a static method(created at runtime). (There is a small difference with stateles and statefull cases, they are resolved via generated method arguments)

https://stackoverflow.com/a/33874965

For instance:

interface Supplier {
   void foo();
}

class A {

   private String name;

   public void doSome() {
      baz(() -> System.out.println(this.name));
   }

   private void baz(Supplier sup){
     sup.foo();
   }
}

or

class A {

   private String name;

   public void doSome() {
      baz(new Supplier() {
         void foo() {
            System.out.println(A.this.name);
         }
      });
   }

   private void baz(Supplier sup){
     sup.foo();
   }
}

I recommend to read this: Java8 Lambdas vs Anonymous classes.

2 of 2
3

The x is not recognized as a field from the Age interface. You have to either do a static import (import static Age.x) or specify where the x comes from:

Age oj1 = () -> System.out.print("Age is "+ Age.x);
🌐
Medium
medium.com › @nagarjun_nagesh › lambda-functions-vs-anonymous-inner-classes-in-java-80893c2214d5
Lambda Functions vs. Anonymous Inner Classes in Java | by Nagarjun (Arjun) Nagesh | Medium
February 23, 2024 - In Java, there are multiple ways to implement functional interfaces, which are interfaces with a single abstract method. Traditionally, anonymous inner classes were used to implement these interfaces. However, with the introduction of Java 8, lambda functions were introduced as a more concise and expressive way to implement functional interfaces.
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › tutorial › java › javaOO › anonymousclasses.html
Anonymous Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Instead of implementing this method ... method. Because the EventHandler<ActionEvent> interface contains only one method, you can use a lambda expression instead of an anonymous class expression....
🌐
DZone
dzone.com › coding › java › from java anonymous class to single line lambda in 3 steps
From Java Anonymous Class to Single Line Lambda in 3 Steps
July 1, 2016 - This means we no longer need the return statement, and have converted the initial anonymous class into a single line lambda. @Test public void thirdLevel() { final String greeting = HELLO_WORLD_EXECUTOR.callHelloWorld(name -> "Hello " + name); Assert.assertEquals("Hello Matthew", greeting); } Grab the source code for this article from GitHub. ... Published at DZone with permission of Matthew Casperson. See the original article here. Opinions expressed by DZone contributors are their own. How to Migrate from Java 8 to Java 17+ Using Amazon Q Developer
🌐
Medium
mcvalls.medium.com › java-lambda-expressions-and-anonymous-classes-3b49489388b5
Java: Lambda Expressions and Anonymous Classes | by Marcelo Valls | Medium
February 22, 2022 - An anonymous class is an expression. It is created when we implement a class in place, like this: As we can see, the SomeInterface interface is also a Functional Interface: it has only one abstract method. As long as the class or interface that we want to implement has only one abstract method, the above code is equivalent to the next one: Because of the functional interface, the compiler knows that the lambda expression is valid.
🌐
TutorialsPoint
tutorialspoint.com › differences-between-anonymous-class-and-lambda-expression-in-java
Differences between anonymous class and lambda expression in Java?\\n
July 10, 2020 - By using a lambda expression, we can declare methods without any name. An anonymous class object generates a separate class file after compilation that increases the size of a jar file while a lambda expression is converted into a private method.
🌐
GeeksforGeeks
geeksforgeeks.org › java › difference-between-anonymous-inner-class-and-lambda-expression
Difference between Anonymous Inner Class and Lambda Expression - GeeksforGeeks
July 11, 2025 - lambda expressions implement the ... to treat functionality as a method argument, or code as data. A function that can be created without belonging to any class....
🌐
Medium
medium.com › @salvipriya97 › anonymous-classes-and-lambda-expressions-19c899b6f441
Anonymous classes and Lambda expressions | by Priya Salvi | Medium
September 8, 2023 - Runnable myRunnable = new Runnable() { public void run() { System.out.println("This is an anonymous class."); } }; ... A lambda expression in Java is a way to write short, concise blocks of code that can be used to implement simple, single-method interfaces (also known as functional interfaces) without the need to create a full-blown anonymous inner class.
Top answer
1 of 10
120

An anonymous inner class (AIC) can be used to create a subclass of an abstract class or a concrete class. An AIC can also provide a concrete implementation of an interface, including the addition of state (fields). An instance of an AIC can be referred to using this in its method bodies, so further methods can be called on it, its state can be mutated over time, etc. None of these apply to lambdas.

I'd guess that the majority of uses of AICs were to provide stateless implementations of single functions and so can be replaced with lambda expressions, but there are other uses of AICs for which lambdas cannot be used. AICs are here to stay.

UPDATE

Another difference between AICs and lambda expressions is that AICs introduce a new scope. That is, names are resolved from the AIC's superclasses and interfaces and can shadow names that occur in the lexically enclosing environment. For lambdas, all names are resolved lexically.

2 of 10
67

Lambdas though a great feature, will only work with SAM types. That is, interfaces with only a single abstract method. It would fail as soon as your interface contains more than 1 abstract method. That is where anonymous classes will be useful.

So, no we cannot just ignore anonymous classes. And just FYI, your sort() method can be more simplified, by skipping the type declaration for p1 and p2:

Collections.sort(personList, (p1, p2) -> p1.firstName.compareTo(p2.firstName));

You can also use method reference here. Either you add a compareByFirstName() method in Person class, and use:

Collections.sort(personList, Person::compareByFirstName);

or, add a getter for firstName, directly get the Comparator from Comparator.comparing() method:

Collections.sort(personList, Comparator.comparing(Person::getFirstName));
🌐
Coderanch
coderanch.com › t › 698612 › java › convert-anonymous-class-lambda
How to convert the anonymous class to lambda? (Features new in Java 8 forum at Coderanch)
September 3, 2018 - The compiler also knows that an EventHandler<ActionEvent> object MUST implement the handle() of the EventHandler interface, because all methods of an interface MUST be implemented. Therefor we can remove that as well. Toss in the lambda operator ->, and voilà we have our lambda expression.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Java 8: Converting Anonymous Classes to Lambda Expressions - Java Code Geeks
June 14, 2016 - For example, in lambda expressions, this and super are lexically scoped, meaning they are relative to the enclosing class, but in an anonymous class, they are relative to the anonymous class itself.
Top answer
1 of 5
48

tl;dr: while it's mostly syntactic sugar, that nicer syntax makes lots of things practical that used to end in endless, unreadable lines of braces and parentheses.

Well, it's actually the other way around as lambdas are much older than Java. Anonymous inner classes with a single method are (were) the closest Java came to lambdas. It's an approximation that was "good enough" for some time, but has a very nasty syntax.

On the surface, Java 8 lambdas seem to be not much more than syntactic sugar, but when you look below the surface, you see tons of interesting abstractions. For example the JVM spec treats a lambda quite differently from a "true" object, and while you can handle them as if they where objects, the JVM is not required to implement them as such.

But while all that technical trickery is interesting and relevant (since it allows future optimizations in the JVM!), the real benefit is "just" the syntactic sugar part.

What's easier to read:

myCollection.map(new Mapper<String,String>() {
  public String map(String input) {
    return new StringBuilder(input).reverse().toString();
  }
});

or:

myCollection.map(element -> new StringBuilder(element).reverse().toString());

or (using a method handle instead of a lambda):

myCollection.map(String::toUpperCase);

The fact that you can finally express in a concise way which would previously be 5 lines of code (of which 3 are utterly boring) brings a real change of what is practical (but not of what is possible, granted).

2 of 5
14

For Java, yes, it's nothing more than a better way of creating an anonymous inner class. This is because of the fundamental decision in java that every bit of byte code has to live within a specific class, which cannot be changed now after decades of legacy code to consider.

However, that is not what lambda expressions really are about. In formalisms where they are native concepts rather than a jump-on-the-bandwagon contortion, lambdas are fundamental building blocks; both their syntax and people's attitude towards them are very different. The example of an anonymous recursive function created purely out of lambdas in Structure and Interpretation of Computer Programs is capable of changing your entire conception of computation. (However, I'm pretty certain that way of learning to program is just to esoteric ever to become a mainstream success story.)