Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • What is Enum type in Java

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 164
    Comment on it

    Enum type is a keyword which is used to represent similar kind of constants or fixed no of elements under one group only. The constants here may be static or final. In java the common use of the Enum keyword is to write Singleton. Enum as a type is more flexible or suitable to be used to represent similar kind of fixed values under a group.

    Note : The value defined under Enum are constants i.e., the of type static and final.

    Enum in java is a keyword or in java Enum is used as type like class and interface to represent the set of constants.

    The Syntax of the Enum is as :

    enum Days
    {
        Monday, Tuesday, Wednesday, Thursday, Friday, Saturday,Sunday
    }
    

    In the above code The enum keyword is used to define an enum Days which have the constants as " Monday, Tuesday, Wednesday, Thursday, Friday, Saturday,Sunday"

    We can use this enum in java classes as :

    public class example
    {
        public static void main(String[] args)
        {
            Days d1 = Days.Monday;
            System.out.println(d1);
    
            Days d2 = Days.Tuesday;
            System.out.println(d2);
    
            System.out.println(Days.Wednesday);
    
            System.out.println(Days.Thursday);
        }
    }
    

    Output
    Monday
    Tuesday
    Wednesday
    Thursday

    In the above code the enum type Days been accessed in the java class by creating the object of the Days or directly the constants of the Days are accessed using dot operator.

 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: