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

JavaObject Oriented ProgrammingProgramming

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
raja
Published on 24-May-2019 12:40:35
Advertisements