Open In App

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

Last Updated : 17 Nov, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • Class (may be abstract or concrete)
  • Interface

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 :

  • To check whether a class is anonymous the best(and the easiest!) way is to use the isAnonymousClass() library function defined in java.lang.Class class.
  • Instances of the class Class represent classes and interfaces in a running Java application.
  • All classes(so their objects) have a getClass() method(inherited from java.lang.Object class) which returns the Class instance representing that particular class.
  • Using the “getClass()” method you can get access of its corresponding Class instance and then you can call its “isAnonymousClass()” method to check whether it is an anonymous class. It returns true if the class is an anonymous class and false otherwise.

Go through the following code:

Java




// 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:

  • Though the first approach is the best and direct way to check if the given class is anonymous or not, we can achieve the same using a different method.
  • This approach uses the fact that anonymous classes don’t have any names.
  • So, if you have an object, you can check whether its class(which is represented as a Class instance) has a name or not.
  • We know that a Class instance can be used to get the metadata about the class represented by this instance.
  • If we have the reference to the Class instance representing the object’s class we can get the name of the class by calling the “getSimpleName()” method.
  • This method returns the simple name of the corresponding class in form of String. So, as expected this method returns an empty string(“”) if the class is an anonymous class. Therefore, we can use this method to do our task.

Java




// 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.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads