Java Enum - The Coding Shala

Home >> Learn Java >> Java Enum

Java Enum

Java Enum is a special type of data type. Enum is used to make collections of constants. It can be used for days of the week or directions or solar system etc. The java enum is final static implicitly. When we define Java enum it creates a class of type enum so we can define java enum inside the class or outside the class it doesn't matter. Java enum can have fields, constructors and methods.
Java Enum - The Coding Shala
Note: Constructors of the enum is always private. if we don't make it private java compiler make private constructor by defaults.

Java enum can be easily used with if-else or switch statements. we can traverse through enum easily. we can't create a new instance of enum because of its private constructor. Enum may implement interfaces but cannot extend any class because it internally extends Enum class.

The below example shows how to define enum and assign value to an enum type-
//HOW TO Define 
public enum Days{
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY
    ;
}


//how to assign a value
Days day = Days.MONDAY;  //assigning value of Days enum type.
Days day1 = Days.FRIDAY;

We can easily iterate through an enum variable, we use .value() that returns the list of values of an enum. the following is an example of this- 


//HOW TO Define 
public enum Days{
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY
    ;
}


class EnumExample{
    public static void main(String[] args){
       for(Days day : Days.values()){
           System.out.print(day+" ");
       }
    }
}



<<<<<<<OUTPUT>>>>>>>>>>
MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY 

we can also use enum in an if-else statement and switch statement. The following example explains this- 
//HOW TO Define 
public enum Days{
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY
    ;
}


class EnumExample{
    public static void main(String[] args){
       Days day = Days.SUNDAY;
       if(day == Days.FRIDAY)
       System.out.println("Last working day of the week");
       else if(day == Days.SUNDAY)
       System.out.println("Just sleep :P");
       switch(day){
           case MONDAY:
               System.out.println("Worst day of the week");
               break;
            case SUNDAY:
                System.out.println("Yeahhh it's Sunday");
                break;
            default:
            System.out.println("check day");
       }
    }
}



<<<<<<<<<<OUTPUT>>>>>>>>>>>>>
Just sleep :P
Yeahhh it's Sunday

As we know enum can have Constructor(private) and methods also. we also can initialize specific values to the enum constants. The enum constants have initial values that start from 0, 1, 2 and so on. Let's take an example of enum that contains constructors, methods, and values. 
//HOW TO Define 
public enum Days{
    MONDAY(1),
    TUESDAY(2),
    WEDNESDAY(3),
    THURSDAY(4),
    FRIDAY(5),
    SATURDAY(6),
    SUNDAY(7)
    ;
    private int value;
    Days(int value){
        this.value = value;
    }
    public int getDay(){
        return this.value;
    }
}


class EnumExample{
    public static void main(String[] args){
       for(Days day : Days.values()){
           System.out.println(day+" "+day.getDay());
       }
    }
}


<<<<<<<<OUTPUT>>>>>>>>>
MONDAY 1
TUESDAY 2
WEDNESDAY 3
THURSDAY 4
FRIDAY 5
SATURDAY 6
SUNDAY 7

Enum class is available in java.lang.Enum and values(), ordinal(), valueOf() methods present in this.
  • ordinal() method used to return index of enum constant.


Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to find sum, product and average of given numbers - The Coding Shala

Single Number 3 LeetCode Solution - The Coding Shala

LeetCode - Number of Good Pairs Solution - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Java Method Overloading - The Coding Shala