Anonymous class in Java

The anonymous class in java is one of the most important topics. As we know anonymous class is part of inner classes in java, that’s why it is also known as an anonymous inner class. So, if you are not familiar with the inner class in java, then I recommend you please read them first. In this post, we will discuss the anonymous inner class in java or anonymous class in java. We will see what the need of anonymous class and advantages of it is.

What is an anonymous class in java?

An inner class that is declared without a name is known as an anonymous class in java. The anonymous class is also known as an anonymous inner class. Because the anonymous class is possible only with the inner class. We can declare an anonymous class(a class without a name) only inside another class. If you’re not familiar with inner class in java then you should read them first. We will see the syntax of an anonymous class.

anonymous class in java

As you can see in the above syntax, we are declaring the anonymous class as well as creating an object of anonymous class.  An anonymous class can be providing body to interface, abstract/concrete class. The anonymous class is used when you want to use any class only once. An anonymous class can declare by the use of a new keyword.

How to create an anonymous class in java?

There are two ways to create an anonymous class in java.

1. Create an anonymous class by use of class
2. Create an anonymous class by use of Interface

1. Create an anonymous class by use of class

You can create an anonymous class by use of another class. It may be an abstract or concrete class.

By use of abstract class

If you are want to create an object of an abstract class. 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 class.

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

By use of concrete class

If you are want to create an object of a concrete class as in anonymous class. Then you must have to override the body of the method while creating the object of that class.

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 by use of 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

Important point:

  • 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

5 thoughts on “Anonymous class in Java”

  1. 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
    }
    }

    Reply
  2. 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);
    }

    }

    Reply

Leave a Comment