๐ŸŽ“ A1Learner
๐Ÿ”
0 of 30 lessons completed

Java Anonymous and Local Classes

An anonymous class is a class with no name that fills in another type in place. A local class is a named class declared inside a method.

An anonymous class fills in an interface in place

Greeter is an interface with one greet method and no named class in this program. The anonymous class writes the method body at the place where the object is made.

public class Main {
  public static void main(String[] args) {
    Greeter greeter = new Greeter() {
      public void greet() {
        System.out.println("Hello from a class with no name");
      }
    };
    greeter.greet();
  }
}

interface Greeter {
  void greet();
}

The same job as a lambda

Both forms fill in the one greet method of the Greeter interface. Both calls print the same line, so the two forms do the same job here.

public class Main {
  public static void main(String[] args) {
    Greeter first = new Greeter() {
      public void greet() {
        System.out.println("Hello");
      }
    };
    Greeter second = () -> System.out.println("Hello");
    first.greet();
    second.greet();
  }
}

interface Greeter {
  void greet();
}

A local class reads a method variable

Welcome is a local class, declared inside the main method. The name variable never changes after it is set, so it is effectively final and Welcome can read it.

public class Main {
  public static void main(String[] args) {
    String name = "Ana";
    class Welcome {
      void show() {
        System.out.println("Welcome, " + name);
      }
    }
    Welcome welcome = new Welcome();
    welcome.show();
  }
}

Example

public class Main {
  public static void main(String[] args) {
    String place = "the harbour";
    class Announcer {
      void announce() {
        System.out.println("News from " + place);
      }
    }
    Speaker anonymous = new Speaker() {
      String style = "loud";

      public void speak() {
        System.out.println("Anonymous speaks in a " + style + " voice");
      }
    };
    Speaker lambda = () -> System.out.println("Lambda speaks");
    anonymous.speak();
    lambda.speak();
    Announcer announcer = new Announcer();
    announcer.announce();
  }
}

interface Speaker {
  void speak();
}

Key points

  • An anonymous class fills in an interface at the exact place where its only use sits.
  • An anonymous class can hold fields of its own.
  • An anonymous class can fill in several methods of its type.
  • A lambda is the short form for an interface with one method to fill in.
  • A variable that never changes after it is set is called effectively final.
  • A local class reads the effectively final variables of the method around it.

In this example, the anonymous Speaker holds its own style field and fills in the speak method with it. The lambda Speaker prints its line with far less code, and the local Announcer reads the place variable from the method around it.

Note

A named class suits a type that many places reuse, while an anonymous class suits one place.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which job suits an anonymous class better than a lambda?