Open In App

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

Anonymous classes are the inner classes with no name. We can’t create the instances of this anonymous class since they have no name.

Anonymous inner class are mainly created in two ways:



Syntax: The syntax of an anonymous class expression is like the invocation of a constructor, except that there is a class definition contained in a block of code.

// Test can be interface,abstract/concrete class
Test t = new Test() 
{
   // data members and methods
   public void test_method() 
   {
      ........
      ........
    }   
};

An object can be created in two ways:



1. By calling the constructor of the class.

GFG gfg = new GFG();

2. By using an anonymous class.

GFG gfg = new GFG(){
    // An anonymous class that extends GFG class
}

Both of the above objects are valid. Now, if for some reasons(debugging, code analysis, etc.) you want to check if the object is created using an anonymous class or not, you can follow one of the following ways:

Approach 1 :

Go through the following code:




// Java program to check if a class is an anonymous class
// using isAnonymous() method
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        // Creating an object by calling its default
        // constructor
        GFG obj = new GFG();
  
        // obj.getClass() returns the Class instance
        // representing obj's class(i.e. it represents the
        // GFG class) The Class instance has a method called
        //"isAnonymousClass()" (defined in java.lang.Class
        //class) Which returns true if obj's class is an
        // anonymous class
        // and false otherwise
        System.out.println(
            "Is obj a anonymous class? :"
            + obj.getClass().isAnonymousClass());
  
        // Creating an object using an anonymous class
        GFG object = new GFG() {
            // This is an anonymous class that extends the
            // GFG class
        }; // Remember to put the semicolon here
  
        System.out.println(
            "Is object an anonymous class? :"
            + object.getClass().isAnonymousClass());
    }
}

Output
Is obj a anonymous class? :false
Is object an anonymous class? :true

Approach 2:




// Java program to check if the class is 
// anonymous or not using getSimpleName() method
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        // Creating a GFG object by calling
        // its default constructor
        GFG obj = new GFG();
  
        // Note that getSimpleName() is defined in
        // java.lang.Class class
  
        // So you need to first call obj's getClass() method
        // and then call getSimpleName() method
  
        System.out.println(
            "Is obj an anonymous class object? :"
            + obj.getClass().getSimpleName().equals(""));
        // Check the above line carefully
  
        // getSimpleName() returns a string
  
        // So the equals() method of String is called
        // To check whether the name of the class
        // is empty or not
  
        // Creating an anonymous class
  
        GFG object = new GFG() {
            // This is an anonymous class that
            // extends the GFG class
        }; // Again remember to put the semicolon here
  
        System.out.println(
            "Is object an anonymous class object? :"
            + object.getClass().getSimpleName().equals(""));
    }
}

Output
Is obj an anonymous class object? :false
Is object an anonymous class object? :true

Note:

  • If you try to use “getName()” method instead of “getSimpleName()” method in the above code, it wouldn’t work.
  • In this case “getName()” would return “GFG$1″(1st anonymous class inside GFG class) which is not empty string.
  • So, the condition in the last line would evaluate to false.
  • If you have an another anonymous class inside the GFG class, “GFG$2″(2nd anonymous class inside GFG class) will be returned if the “getName()” method is called.

Article Tags :