Open In App

Java program without making class

Last Updated : 03 Jul, 2017
Improve
Improve
Like Article
Like
Save
Share
Report

Can you run Java program without making class?

The idea is to us enum is Java. Every enum constant is always implicitly public static final. Since it is static, we can access it by using enum Name. Since it is final, we can’t create child enums.

We can declare main() method inside enum. Hence we can invoke enum directly from the Command Prompt.




// A Java program to demonstrate that we can have
// main() inside enum class.
enum Color
{
    RED, GREEN, BLUE;
   
    // Driver method
    public static void main(String[] args)
    {
        Color c1 = Color.RED;
        System.out.println(c1);
    }
}


Output :

RED

Please note that enum also uses class internally. The purpose of this post is create an interesting question where user doesn’t have to create a class explicitly.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads