ADVERTISEMENT
ADVERTISEMENT

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

In this program, we will check the anonymous class using the isAnonymousClass() method and print the appropriate message.

Program/Source Code:

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 »


ADVERTISEMENT


ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.