Java.lang Package Classes

Java Class - isAnonymousClass() Method



The java.lang.Class.isAnonymousClass() method returns true if and only if the underlying class is an anonymous class.

Syntax

public boolean isAnonymousClass()

Parameters

No parameter is required.

Return Value

Returns true if and only if this class is an anonymous class.

Exception

NA.

Example:

In the example below, the java.lang.Class.isAnonymousClass() method is used to check if the given class is an anonymous class or not.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    MyClass x = new MyClass();
    Class cls = x.getClass();

    //print the name of the class
    String name = cls.getName();
    System.out.println("Class Name: " + name);

    //check if the class is an anonymous class
    Boolean result = cls.isAnonymousClass();
    System.out.println("Is it AnonymousClass?: " + result);
  }
}

The output of the above code will be:

Class Name: MyClass
Is it AnonymousClass?: false

❮ Java.lang - Class