The anonymous class in java is a key topic to master. Since an anonymous class belongs to the family of inner classes in java, it is also called an anonymous inner class. If the inner class in java is new to you, it is worth reading that topic first. This post covers the anonymous inner class in java (or anonymous class in java), why you need one, and what advantages it brings.
What Is an Anonymous Class in Java?
An inner class declared without a name is called an anonymous class in java. It is also referred to as an anonymous inner class, because an anonymous class can only exist together with an inner class. In other words, a class without a name can be declared only inside another class. If inner class in java is unfamiliar to you, read that topic first. Next we look at the syntax of an anonymous class.

As the syntax above shows, you declare the anonymous class and create its object at the same time. An anonymous class can supply a body for an interface or for an abstract or concrete class. It is useful when a class is needed only once, and it is declared with the new keyword.
How to Create an Anonymous Class in Java
You can create an anonymous class in java in two ways.
1. Create an anonymous class using a class
2. Create an anonymous class using an interface
1. Create an anonymous class using a class
You can build an anonymous class from another class, whether that class is abstract or concrete.
Using an abstract class
If you need an object of an abstract class, an anonymous class can provide it. Inside the anonymous class you must define the method body at the point where the object is created.
abstract class Student
{
abstract void record();
}
class AnonymousInnerExample
{
public static void main(String args[])
{
Student obj = new Student()
{
void record()
{
System.out.println("This method is defined in anonymous class");
}
};
obj.record();
}
}
Output: This method is defined in anonymous class
Using a concrete class
If you want an object of a concrete class through an anonymous class, you must override the method body while creating that object.
class Student
{
void record()
{ }
}
class AnonymousInnerExample
{
public static void main(String args[])
{
Student obj = new Student()
{
void record()
{
System.out.println("This method is defined in concrete class");
}
};
obj.record();
}
}
Output: This method is defined in anonymous class
2. Create an anonymous class using an interface
You can create an anonymous class by use of Interface. If you are want to create an object of an Interface. You can use an anonymous class. In the anonymous class, you must have to define the body of the method while creating the object of that Interface.
interface Student
{
void record();
}
class AnonymousInnerExample
{
public static void main(String args[])
{
Student obj = new Student()
{
public void record()
{
System.out.println("This method is defined in anonymous class");
}
};
obj.record();
}
}
Output: This method is defined in anonymous class
Key points to remember:
- Anonymous class has no name
- It can be instantiated only once
- It is usually declared inside a method or a code block, curly braces ending with a semicolon.
- It is accessible only at the point where it is defined.
- It does not have a constructor simply because it does not have a name
- It cannot be static


members defined only in anonymous class are accessible only inside the anonymous class.
class Anonymous {
void record() {}
}
class Program {
public static void main(String args[]) {
Anonymous obj = new Anonymous() {
int x=100;
void record() {
System.out.println(“in anonymous class: ” +x());
}
int x(){
return x;
}
};
obj.record();
// int a =obj.x; //error
// int b =obj.x(); // error
}
}
Good job 🙂
It’s not true, sir.
as you can see below code it is working fine.
public class Main
{
Main b = new Main();
public static void main(String[] args) {
System.out.println(“Hello World”);
class Inner{
int a = 10;
private Inner () {
//System.out.println(a);
}
}
Inner a = new Inner();
int x = a.a;
System.out.println(x);
}
}
the class Inner here you have created is not anonymous class ,its a normal nested class.
Hi,
can you explain about “Anonymous object”?
Sure