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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 469
    Comment on it

    Enum:

    It is a special data type used to define fixed set of constants . Enum can contain constants, methods etc. We can use enum with switch statement easily. Enum keyword is used to create enums.

    Example:

        class EnumDemo{  
        public enum Constant { Constant1, Constant2, Constant3, Constant4 }  
          
        public static void main(String[] args) {  
        for (Constant c : Constant.values())  
        System.out.println(c);  
          
        }}  

    It will output:

    Output:Constant1
           Constant2
           Constant3
           Constant4

    Using Enum with Switch Statement:

        class EnumDemo1{  
        enum Days{ SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY}  
        public static void main(String args[]){  
        Day days=Days.MONDAY;  
          
        switch(days){  
        case SUNDAY:   
         System.out.println("sunday");  
         break;  
        case MONDAY:   
         System.out.println("monday");  
         break;  
        default:  
        System.out.println("other day");  
        }  
        }}  

    It will output : monday

     

 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: