Java Program to Check if a given Class is an Anonymous Class



The type of nested class that has no name is called an anonymous class. Before diving into a Java program to check if a given class is an anonymous class, we need to understand what is a nested class. Let's discuss this in detail.

What is a Java Nested Class?

A class created within another class in Java is called a nested class. We know that we cannot declare a Java class private, but we can define a nested class as private. There are two types of nested classes in Java: static and non-static.

The static nested class is defined using the static modifier, but a Non-static nested class is not defined using the static keyword, and it is also known as the inner class.

What is Inner Class?

Non-static or Inner class can access all the static and non-static members of its outer class. It can also access the members that are private, but an outer class cannot access the private members of an inner class.

There are two main types of inner classes:

  • Local Inner Class: It is defined inside a method block. We can also define in loops. Just like the local variables and methods, its scope lies within the method or loop block where it is defined.

  • Anonymous Inner Class: It is the type of inner class that has no name. Its declaration and initialization happen at the same time. It is mainly used for overriding methods. We can declare an anonymous inner class abstract.

Program to Check Anonymous Class in Java

We will use the following inbuilt methods of Java to check if a given class is anonymous or not -

  • getClass(): It returns the class of the specified object.

  • isAnonymousClass(): It returns true if a given class is anonymous, otherwise false.

Example

Let's see a Java program that uses the getClass() and isAnonymousClass() methods together to check an anonymous class.

import java.lang.*;
abstract class Anonymous { 
   // abstract class
   abstract public void display(); 
   // abstract method
}
public class Main {
   public static void main(String args[]) {
      Anonymous obj = new Anonymous() { 
         // anonymous inner class
         public void display() { 
            // overriding display() method
            System.out.println("I am display method of anonymous inner class");
         }
      };
      obj.display(); 
      // calling display() method
      // fetching the name of class of object ?obj' i.e. Anonymous
      Class cls = obj.getClass(); 
      // Checking if class 'Anonymous' is anonymous class or not
      boolean isAnonymous = cls.isAnonymousClass();
      // Printing the Boolean result
      System.out.println("Is Anonymous a Anonymous class: " + isAnonymous);
   }
}

Since the Anonymous class is an anonymous inner class, the program will return TRUE.

I am display method of anonymous inner class
Is Anonymous a Anonymous class: true
Updated on: 2025-06-19T16:15:26+05:30

174 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements