Java program without making class

Java 8Object Oriented ProgrammingProgramming

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
raja
Updated on 25-Jun-2020 14:23:05

Advertisements