Java program without making class


Is it possible to create and run a Java program without any class? The answer is Yes. Trick is to use enum instead of Class. Although enum is used to represent public static constant. It can have static main method as well. See the example below −

Example

 Live Demo

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

Programming in C
Programming in Java
Programming in PHP

Updated on: 25-Jun-2020

400 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements