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: 2023-11-17T14:26:55+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements