Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Enum with a main method in java

    • 0
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 250
    Comment on it

    Enum In java

    In java we use of enum is not confined to declaring set of constants , introduced in J2SE 5.0, is useful when you want a variable to hold only a predetermined set of values. Just like a class, an enum can have constructors, methods, and variables in addition to the constants.

    The most interesting fact about enum is enum can be used as a stand alone application as we can define the main method in it , the following code describe the use of enum in java with main method.

    enum Cars {
        THAR,XUV,SCORPIO;
    
        static public void main(String[] args) {
            for (Cars name : Cars.values()) 
                   System.out.print(name + " ");
       }
    
    }
    

    In the following code the enum Cars is created with three constants THAR, XUV, SCORPIO , it have a main method to print the all constants of the enum.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: