Anonymous Inner Class Handlers
Tags: java, programming, learning, beginners
Published: 2024-06-24 20:17:26

An anonymous inner class is an inner class without a name. It combines defining an inner class and creating an instance of the class into one step. Inner-class handlers can be shortened using _anonymous inner classes_. The inner class in ControlCircle.java [here](https://dev.to/paulike/registering-handlers-and-handling-events-3j7e) can be replaced by an anonymous inner class as shown below.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/96y2app4eheisdoo3tq5.png)

The syntax for an anonymous inner class is shown below

`new SuperClassName/InterfaceName() {
// Implement or override methods in superclass or interface
// Other methods if necessary
}`

Since an anonymous inner class is a special kind of inner class, it is treated like an inner class with the following features:

- An anonymous inner class must always extend a superclass or implement an interface, but it cannot have an explicit **extends** or **implements** clause.
- An anonymous inner class must implement all the abstract methods in the superclass or in the interface.
- An anonymous inner class always uses the no-arg constructor from its superclass to create an instance. If an anonymous inner class implements an interface, the constructor is **Object()**.
- An anonymous inner class is compiled into a class named **OuterClassName$n.class**. For example, if the outer class **Test** has two anonymous inner classes, they are compiled into **Test$1.class** and **Test$2.class**.

The program below gives an example that handles the events from four buttons, as shown in Figure below.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7u1zezn50xusnfvtlzb1.png)

```
package application;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class AnonymousHandlerDemo extends Application {
	@Override // Override the start method in the Application class
	public void start(Stage primaryStage) {
		// Hold four buttons in an HBox 
		HBox hBox = new HBox();
		hBox.setSpacing(10);
		hBox.setAlignment(Pos.CENTER);
		Button btNew = new Button("New");
		Button btOpen = new Button("Open");
		Button btSave = new Button("Save");
		Button btPrint = new Button("Print");
		hBox.getChildren().addAll(btNew, btOpen, btSave, btPrint);
		
		// Create and register the handler
		btNew.setOnAction(new EventHandler<ActionEvent>() {
			@Override // Override the handle method
			public void handle(ActionEvent e) {
				System.out.println("Process New");
			}
		});
		
		btOpen.setOnAction(new EventHandler<ActionEvent>() {
			@Override // Override the handle method
			public void handle(ActionEvent e) {
				System.out.println("Process Open");
			}
		});
		
		btSave.setOnAction(new EventHandler<ActionEvent>() {
			@Override // Override the handle method
			public void handle(ActionEvent e) {
				System.out.println("Process Save");
			}
		});
		
		btPrint.setOnAction(new EventHandler<ActionEvent>() {
			@Override // Override the handle method
			public void handle(ActionEvent e) {
				System.out.println("Process Print");
			}
		});
		
		// Create a scene and place it in the stage
		Scene scene = new Scene(hBox, 300, 50);
		primaryStage.setTitle("AnonymousHandlerDemo"); // Set title
		primaryStage.setScene(scene); // Place the scene in the stage
		primaryStage.show(); // Display the stage
	}
	
	public static void main(String[] args) {
		Application.launch(args);
	}
}

```

The program creates four handlers using anonymous inner classes (lines 25–51). Without using anonymous inner classes, you would have to create four separate classes. An anonymous handler works the same way as that of an inner class handler. The program is condensed using an anonymous inner class.

The anonymous inner classes in this example are compiled into
**AnonymousHandlerDemo$1.class**, **AnonymousHandlerDemo$2.class**,
**AnonymousHandlerDemo$3.class**, and **AnonymousHandlerDemo$4.class**.