Home »
Java Programs »
Java Class and Object Programs
Java program to check anonymous class using isAnonymousClass() method
Java example to check anonymous class using isAnonymousClass() method.
Submitted by Nidhi, on April 30, 2022
Problem statement
In this program, we will check the anonymous class using the isAnonymousClass() method and print the appropriate message.
Java program to check anonymous class using isAnonymousClass() method
The source code to check anonymous class using the isAnonymousClass() method is given below. The given program is compiled and executed successfully.
// Java program to check anonymous class using
// isAnonymousClass() method
public class Main {
public static void main(String[] args) {
Class < ? extends Object > cls = new Object() {}.getClass();
boolean res = cls.isAnonymousClass();
if (res)
System.out.println("It is an anonymous class.");
else
System.out.println("It is not an anonymous class.");
}
}
Output
It is an anonymous class.
Explanation
In the above program, we created a public class Main that contains a main() method. The main() method is the entry point for the program. Here, we checked the anonymous class using the isAnonymousClass() method. The isAnonymousClass() method returns true in the case of an anonymous class otherwise it returns false.
Java Class and Object Programs »