In Java, a nested class without a name is called an anonymous class.
A nested class is one that is enclosed within another class. Because anonymous classes are embedded within other classes, they are also known as anonymous inner classes.
isAnonymousClass()
methodThe isAnonymousClass()
method in Java checks whether a class is an anonymous class or not.
public boolean isAnonymousClass()
This method doesn’t take any parameters.
This method returns a boolean value. If the class is anonymous, it returns true
. Otherwise, it returns false
.
public class Main{ public static void example1(){ System.out.println(Main.class + " is anonymous - " + Main.class.isAnonymousClass()); } public static void example2(){ Class<?> runtimeClass = new Object() {}.getClass(); System.out.println(runtimeClass + " is anonymous - " + runtimeClass.isAnonymousClass()); } public static void main(String[] args) { example1(); example2(); } }
example1()
where we check if the Main
class is anonymous or not using the isAnonymousClass()
method.example2()
that uses the isAnonymousClass()
method to determine whether the runtime class of the Object
class is an anonymous class or not.example1()
and example2()
methods.RELATED TAGS
CONTRIBUTOR
View all Courses