English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Program Without Class

Can you create and run a Java program without any classes? The answer is yes. The trick is to use an enum instead of a class. Although enums are used to represent public static constants, they can also have a static main method. See the example below-

Example

public enum Tester {
   C, Java, PHP;
   public static void main(String[] args) {
      System.out.println("Programming in "); + Tester.C.toString());
      System.out.println("Programming in "); + Tester.Java.toString());
      System.out.println("Programming in "); + Tester.PHP.toString());
   }
}

Output Result

Programming in C
Programming in Java
Programming in PHP