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

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

PHPz
PHPzforward
2023-09-05 19:21:07567browse

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

Yes, we can use anonymous classes to create a class without a name.

Anonymous classes are inner classes without a name, instances of which are created when the class itself is created. These classes are created somewhat differently than normal classes.

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

The above is the detailed content of Is it possible to create a class without a name in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete