Replace Anonymous Java Class with Lambda Expression

In this Java tutorial I am going to share with you how to create an Anonymous java class and also how to Replace Java Anonymous Class with a shorter lambda expression.

Let’s start by creating and interface which we will use in this tutorial. Our anonymous java class will implement a single method defined in this java interface. I will call my java interface UserService.

UserService Java Interface

package com.appsdeveloperblog;

 * @author skargopolov
public interface UserService {
    void greetUser(String greating);

Next I will create a Java class with public static void main method which will make use of this UserService interface to create an anonymous Java class.

Create Anonymous Class in Java

Below is an example of how to create anonymous java class which implements UserService interface I have mentioned above. This is a usual way of creating anonymous java classes and a little bit below we will learn how to use lambda expressions to create a shorter version.

public class UserServiceClient {
    public static void main(String args[])
        // Create Inner Class as usual 
        UserService userService = new UserService(){
            public void greetUser(String id) {
                 System.out.println("Deleting user with id " + id);
        userService.greetUser("Hello Sergey Kargopolov");

Replace Anonymous Java class with Lambda Expression

Below is a an example of lambda expression which replaces anonymous Java class.

// Use lambda Expression
UserService userServiceWithLambda = (String id) -> { 

userServiceWithLambda.greetUser("Hello Sergey Kargopolov 1");

We can make this lambda expression even shorter by skipping the data type of method argument:

// We can skip the data type of method argument 
 UserService userServiceWithLambda2 = (id) -> { 
userServiceWithLambda2.greetUser("Hello Sergey Kargopolov 2");

We can even simplify it further and remove the parentheses around the method argument name:

// We can skip parentheses 
 UserService userServiceWithLambda3 = id -> { 
userServiceWithLambda3.greetUser("Hello Sergey Kargopolov 3");

If method body is only one line we can remove the curly brackets of the method body:

// We can skip the curly brackets if the body of the method is only one line
  UserService userServiceWithLambda4 = id -> System.out.println(id); 
 userServiceWithLambda4.greetUser("Hello Sergey Kargopolov 4");

And finally the shortest version of this lambda expression is with the use of Method Reference

  // Lambda expression with Method Reference 
 UserService userServiceWithLambda5 = System.out::println;
 userServiceWithLambda5.greetUser("Hello Sergey Kargopolov 5");

Now, if you compare this last version of lambda expression which uses Method Reference with the very first version of Java code on this page that creates an anonymous Java class the usual way, with the new keyword, you will notice a big difference. Using Lambda expression we create much shorter and concise code. Although, if you are new to Lambda expressions then it becomes very challenging to read this much shorter code :).

If you want to stay unto date with current Java versions and learn more about new Java features and Lambda expressions, check out the books and video lessons below.

Learn Java Programming – Books

Learn Java Programming – Video Courses

Leave a Reply

Your email address will not be published. Required fields are marked *