HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

7) Lambda Expressions & Methods References Lesson

Primer: What is an Anonymous Class in Java

6 min to complete · By Ryan Desmond

An anonymous class in Java is a class without a specified name that is defined and instantiated simultaneously at the point of use. Anonymous classes are often employed when a simple, one-time-use class is needed for a short and specific purpose.

In order to understand lambda expressions properly, you must first understand the concept of anonymous inner classes.

What is a Java Inner Class

Whenever a class is created within another, it can be referred to as an inner class; in other words, an inner class is a class within another class!

Since an anonymous class is the in-line implementation of an interface, it is almost always an inner class.

Why Use an Anonymous Class

An anonymous inner class implements an interface without creating a new class that implements said interface. Even though that may seem confusing, below, you're going to review what you already know about interfaces and see how the anonymous inner class can save time and code.

Without an Anonymous Class

First, take a look at this interface:

public interface ExampleInterface {
  public double calculate(double a, double b);
} 

In order for you to use this interface, you would typically create a new class that implements this interface. For instance:

public class ExampleClass implements ExampleInterface {    
  @Override
  public double calculate(double a, double b) {
    if (a > b){
      return a;
    } else {
      return b;
    }
  }
} 

The code above should be familiar; it's just an interface and a class that implements said interface. And you would use it by creating an object of this class and then invoking the calculate method:

public class Demo {
  public static void main(String[] args){
    ExampleClass obj = new ExampleClass();
    double largerNumber = obj.calculate(5, 8);
    System.out.println("The larger number is: " + largerNumber);
  }
} 

All looks good, ya? Ya, it is all good. That all works. However, it is a little shorter with an anonymous inner class.

With Anonymous Inner Class

Taking the example above, you can replace the second step of creating the ExampleClass implements ExampleInterface by creating what is known as an "anonymous inner class". To do this, you simply create a new object of the interface - which is typically illegal in Java unless you immediately implement all abstract methods in said interface. For example:

public class AnonymousInnerClass {
  public static void main(String[] args) {
    // this is an example of an anonymous inner class
    // you are creating an object of the interface 
    // by immediately implementing 
    // the abstract method within that interface
    ExampleInterface obj = new ExampleInterface() {
      @Override
      public double calculate(double a, double b) {
        if (a > b ){
          return a;
        } else {
          return b;
        }
      }
    };

    double largerNumber = obj.calculate(5, 8);
    System.out.println("The larger number is: " + largerNumber);
  }
} 

As you can see, you have skipped the step of creating the class that implements the interface, and you've gone straight to creating an object of the interface - this is known as an anonymous inner class. The only way to create an object of an interface (without using a dependency injection framework such as Spring) is by implementing the abstract methods in-line immediately upon creating that object, as you have done in the example above.

Now that you understand this, you're one step closer to being able to discuss lambda expressions, which are basically an alternative to creating anonymous inner classes.

Summary: What is an Anonymous Class in Java

  • An anonymous class is created and initiated at the same time
  • An inner class is a class created inside of another
  • Anonymous classes are very often anonymous inner classes
  • An anonymous class can reduce code compared to creating an interface, creating a class that implements that interface, and then creating another class that uses the class that implements the interface (that's a mouthful!)