Anonymous Classes in Java
Anonymous Classes (or Anonymous Inner Classes) are nameless classes, more precisely, a class with no name that’s defined and initialized at the same time.
Anonymous classes offer a compact notation if you do not want to create your own class for the implementation of an interface or an abstract class. In Java 8, lambda expressions can be seen as anonymous classes of functional interfaces.
We initialize the Anonymous Classes with the new operator.
They are often used when you want to create a unique object on the fly. This is the case, for example, in GUIs with EventListeners or simple threads.
Anonymous classes extend existing classes or implement interfaces without using extends. If we pass the parameters during creation, the superclass must have the appropriate constructor. The anonymous class itself does not declare a constructor.
Example : Anonymous Classes in Java
public class AnonymousClass { public static void main(String[] args) { new Person("Thomas") { void printName() { System.out.println("My name is: " + this.getName()); } }; } } class Person { private String name; public Person(String name) { this.name = name; printName(); } void printName() { System.out.println(name); } String getName() { return name; } }
Output:
My name is: Thomas
In the above example, We have created an anonymous class of type Person in the main method. We pass the string to the constructor and the method “printName()” is overwritten in its body. In the superclass it prints the value of the variable name on the console. In the overwritten version of the anonymous class, we expand this string a bit and we call the method “getName()” of the superclass.
Interfaces are often implemented in anonymous classes.
Example : Anonymous class when implementing an interface
To implement an interface or to inherit from a class you normally create a new class which you can instantiate:
public class MyTask implements Runnable{ @Override public void run() { System.out.println("Running!"); } } new Thread(new MyTask()).start();
The above example creates the MyTask class, which implements the Runnable interface. That means the class has to implement the method run(). A new instance of the class is given to the thread.
Implementing the interface as above is basically not a bad way, but a separate class must be created for each task. This sometimes demands a lot of work, especially if the task is very small and is only used at one place.
Now if you are not doing that for every minimal task, you can have an inner class instead of the runnable object.
new Thread(new Runnable() { @Override public void run() { System.out.println("Running!"); } }).start();
Syntax of an anonymous class
An anonymous class consists of a constructor call and a code block, so in the minimal case
new Object(){};
The Code-Bloc {} can now contain any code such as variables and methods.
The anonymous class must definitely have implementations of the abstract methods of the interface or the super class (e.g. run() method in the example above). Optionally, non-abstract methods can also be overwritten.
Since Java 8.0, this has also become much clearer thanks to lambda expressions.
Lambda expressions instead of anonymous classes
If the interface of the anonymous class is a functional interface, i.e. only contains an abstract method, a lambda expression can also be used instead of the anonymous class.
Anonymous class:
new Thread(new Runnable() { @Override public void run() { System.out.println("Running!"); } }).start();
becomes the lambda expression:
new Thread(()-> System.out.println("Running!")).start();
It should be noted that a lambda expression is not an anonymous class. Even if most anonymous classes can be replaced by lambdas, a lambda expression is always stateless, anonymous classes can definitely contain a status.