⬅ Previous Topic
Java Nested Static ClassNext Topic ⮕
Java Singleton Class⬅ Previous Topic
Java Nested Static ClassNext Topic ⮕
Java Singleton ClassIn Java, we often create a class, give it a name, define its methods, then use it. But what if you only need the class once — just for a quick tweak or override? That’s where anonymous classes come in.
An anonymous class in Java is a class without a name. You define and instantiate it all at once, usually when you need to override a method of an existing class or interface for one-time use.
Anonymous classes help reduce boilerplate code when working with simple interface implementations or abstract class extensions. They're especially useful in GUI event handling, callbacks, and quick tweaks where defining a full class would feel redundant.
The structure of an anonymous class looks like this:
Type instance = new Type() {
// overridden methods
};
Here, Type
can be an interface or a class (usually abstract).
interface Greeting {
void sayHello();
}
public class AnonymousDemo {
public static void main(String[] args) {
Greeting greet = new Greeting() {
@Override
public void sayHello() {
System.out.println("Hello from Anonymous Class!");
}
};
greet.sayHello();
}
}
Hello from Anonymous Class!
Here, we didn’t create a separate class to implement Greeting
. Instead, we provided the method body right where the object is created. Compact, clear, and direct.
abstract class Animal {
abstract void sound();
}
public class Zoo {
public static void main(String[] args) {
Animal dog = new Animal() {
@Override
void sound() {
System.out.println("Woof! Woof!");
}
};
dog.sound();
}
}
Woof! Woof!
The anonymous class provides an implementation for the abstract method sound()
and we’re ready to use it — no need to name the subclass at all.
class Person {
String name;
Person(String name) {
this.name = name;
}
void introduce() {
System.out.println("Hi, I'm " + name);
}
}
public class Demo {
public static void main(String[] args) {
Person p = new Person("Alice") {
@Override
void introduce() {
System.out.println("Hi there, I'm " + name + ", and I love Java!");
}
};
p.introduce();
}
}
Hi there, I'm Alice, and I love Java!
Yes — you can even pass parameters to a superclass constructor when using an anonymous class. And then, override the behavior in place.
Runnable
interfacepublic class ThreadDemo {
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
System.out.println("Running in a thread");
}
};
new Thread(r).start();
}
}
Running in a thread
Java 8 introduced lambda expressions which are often used in place of anonymous classes when working with functional interfaces.
// Anonymous class
Runnable r1 = new Runnable() {
public void run() {
System.out.println("From Anonymous Class");
}
};
// Lambda expression
Runnable r2 = () -> System.out.println("From Lambda");
r1.run();
r2.run();
From Anonymous Class
From Lambda
Java anonymous classes allow you to define and instantiate a class in a single expression. They’re used for quick, throwaway implementations, especially when working with interfaces or abstract classes. While not always ideal for complex behavior, they shine when used sparingly for localized functionality.
Runnable r = new Runnable() {
public void run() {
System.out.println("Hello from anonymous thread!");
}
};
r.run();
⬅ Previous Topic
Java Nested Static ClassNext Topic ⮕
Java Singleton ClassYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.