Is it possible to create a class without a name in Java?


Yes, we can create a class without a name using the Anonymous class.

Anonymous class is an inner class which does not have a name and whose instance is created at the time of the creation of class itself and these classes are somewhat different from normal classes in its creation.

Example

public class Anonymous {
   public void show() {}
   public static void main(String args[]) {
      Anonymous a = new Anonymous() {
         public void show() {
            System.out.println("Anonymous Class");
         }
      };
      a.show();
   }
}

Output

Anonymous Class

Updated on: 17-Nov-2023

829 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements