Home > Java > javaTutorial > body text

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

PHPz
Release: 2023-09-05 19:21:07
forward
566 people have browsed it

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();
   }
}
Copy after login

Output

Anonymous Class
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template