Anonymous Classes in Java and When to Use Them?
An anonymous class in Java is a class without a name that is both defined and instantiated in a single expression. Developers commonly use anonymous classes to create instances of classes or implement interfaces without writing a separate named class. This helps in writing cleaner and more concise code, especially for short-term or one-time use cases in Java programming.

Characteristics of Anonymous classes
Anonymous class is
- A local class without a name
- often used to provide short term implementations of interface or abstract class
- Mainly used in event handling thread creation or callback mechanism
Example: Example without Anonymous class
1. Greeting.java (Interface)
package com.test.rkdigital.school;
public interface Greeting {
void sayHello();
}
2. GreetingImpl.java (Interface Implementation)
package com.test.rkdigital.school;
public class GreetingImpl implements Greeting{
@Override
public void sayHello() {
// TODO Auto-generated method stub
System.out.println("I am from sayHello");
}
}
3. DemoTest.java
package com.test.rkdigital.school;
public class DemoTest {
public static void main(String[] args) {
Greeting obj=new GreetingImpl();
obj.sayHello();
}
}
Women's Maroon Poly Crepe A-Line Kurta Set With Dupatta
- Kurta Material: Poly Crepe || Pant Material: Poly Crepe || Dupatta Material: Georgette
- Style: Straight kurta || Calf Lenth with 3/4 Sleeve
- This Product Ready Chest Size: Small= 36 (Inch) || Medium=38 (Inch) || Large=40 (Inch) || Xl=42 (Inch) || XXl=44 (Inch)
Example: Example with Anonymous class
package com.test.rkdigital.school;
public interface Greeting {
void sayHello();
}
- Defines an interface named Greeting
- It has one method sayHello() , which does not return anything and does not take any parameters .
2. DemoTest.java
package com.test.rkdigital.school;
public class DemoTest {
public static void main(String[] args) {
Greeting obj=new Greeting() {
@Override
public void sayHello() {
// TODO Auto-generated method stub
System.out.println("Hello from Anonymous class Implementation");
}
};
obj.sayHello();
}
}
Explanation of Above code :
Public class Main{
Public static void main(String[] args){
- Start of class definition named main
- It contains the main() method , which is the entry point of any java application
- main() method where the program starts running
- String[] args allows command line argument to be passed to the program
Greeting greeting = new Greeting(){
- We are creating an object of an anonymous class that implements the greeting interfaces .
- new Greeting() start the instantiation and the {..} block defines the body of the anonymous class.
- This class does have a name and is used only once
public void sayHello(){
System.out.println("Hello from Anonymous class");
}
};
- closes the anonymous class definition
Anonymous Inner classes
An anonymous inner class in Java is a way to define and instantiate a class at the same time without giving it a name. In general, it is commonly used to implement interfaces or extend classes for one-time use, especially when the implementation is short and therefore not reused elsewhere. Moreover, this approach becomes particularly useful when you need to override multiple methods or when you want to include additional logic inline, as a result making the code more structured and efficient.
Lambda Expression
A lambda expression in Java is a concise way to represent a method using an expression and is a core feature of functional programming in Java. It can only be used with functional interfaces, which are interfaces that contain exactly one abstract method. Lambda expressions simplify code by removing boilerplate syntax, making programs more readable and easier to maintain.
Thread Code with Anonymous Inner Class in Java
package com.test.rkdigital.school.solid;
public class ThreadDemoAnonymousInner {
public static void main(String[] args) {
Runnable r= new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<=10;i++) {
System.out.println("child thread !");
}
}
};
Thread t=new Thread(r);
t.start();
for (int i = 0; i < 10; i++) {
System.out.println("Main Thread!!!");
}
}
}
Thread Code with Lambda Expressions in Java
package com.test.rkdigital.school.solid;
import java.util.Iterator;
public class ThreadDemo {
public static void main(String[] args) {
Runnable r= ()-> {
for(int i=0;i<=10;i++) {
System.out.println("child thread !");
}
};
Thread t=new Thread(r);
t.start();
for (int i = 0; i < 10; i++) {
System.out.println("Main Thread!!!");
}
}
}