Table of contents
Enums are a set of named constants . It means we cannot change the value once it is assigned . Every enum constant is by default public , static and final . Enum Constant is also known as Enumerated constant or named constant . The enum was introduced in Java 5 . When you are creating a enum by default it is extending Enum class .
Declaration of Enum
enum Direction { EAST, WEST, NORTH, SOUTH; }
Internally java compiler converts the enum to below format :
class Direction { public static final Direction EAST = new Direction(); public static final Direction WEST = new Direction(); public static final Direction NORTH = new Direction(); public static final Direction SOUTH = new Direction(); }
Accessing enum constants
Enum constants are by default static hence we can access them using the name .
enum Direction { EAST, WEST, NORTH, SOUTH; } public class Demo { public static void main(String[] args) { Direction direction = Direction.EAST; System.out.println(direction); } } Output : EAST
Can we create the enum objects explicity ?
No , we cannot . Enum constructor is private in nature . We cannot create object for classes whose constructors are private in nature hence we cannot create object for enum explicitly . In the above example there are 4 named constants ( EAST, WEST, NORTH, SOUTH ) which means there are 4 instances of enum class created internally . If the enum constructor was public then we could create infinite number of instances which makes no sense at all .
Can we create a enum with different types ?
Yes we can do that . But we have to keep in mind enums are a set of likely types . We should not mix two different types in one enum , it will create a lot of confusion to the programmers . Consider the example below :
enum Direction { EAST, WEST, NORTH, SOUTH, SUMMER, WINTER; } public class Demo { public static void main(String[] args) { Direction direction = Direction.SUMMER; System.out.println(direction); } } Output : SUMMER
The above code works properly but from business perspective it do not make any sense . As we have mixed directions and seasons together it causes a lot of confusions . So do not try to mix different types in one enum . Instead create a separate enum for Seasons .
enum Direction { EAST, WEST, NORTH, SOUTH; } enum Season { SUMMER, WINTER, RAINY, SPRING; } public class Demo { public static void main(String[] args) { Direction direction = Direction.EAST; System.out.println(direction); Season season = Season.WINTER; System.out.println(season); } } Output : EAST WINTER
Enum Constructor
From above examples we have seen when we print named constant the constant name is printed [ EAST , WEST , NORTH , SOUTH ] . But suppose we have to print some different value . For Ex : With Constant “EAST” we want to bind a value as “East” . Is it possible ? Yes It is possible , we can achieve it using constructors in enum .
name method
We can get the constants name using name method . Check the example below :
enum Direction { EAST("East"), WEST("West"), NORTH("North"), SOUTH("South"); String value; Direction(String value) { this.value = value; } } public class Demo { public static void main(String[] args) { String upperCaseDirection = Direction.EAST.name(); System.out.println(upperCaseDirection); } } Output : EAST
value property
We can get the value attached to the enum constant using property value . Check the example below :
enum Direction { EAST("East"), WEST("West"), NORTH("North"), SOUTH("South"); String value; Direction(String value) { this.value = value; } } public class Demo { public static void main(String[] args) { String lowerCaseDirection = Direction.EAST.value; System.out.println(lowerCaseDirection); } } Output : East
Enum Methods
values
We can get all the instances of enum using values() method .
enum Direction { EAST("East"), WEST("West"), NORTH("North"), SOUTH("South"); String value; Direction(String value) { this.value = value; } } public class Demo { public static void main(String[] args) { Direction[] directions = Direction.values(); for (Direction direction: directions) { System.out.println(direction.name() + "-" + direction.value); } } } Output : EAST-East WEST-West NORTH-North SOUTH-South
ValueOf
We can get the instance of a enum , if we just know the constant name by making use of a valueOf() method .
enum Direction { EAST("East"), WEST("West"), NORTH("North"), SOUTH("South"); String value; Direction(String value) { this.value = value; } } public class Demo { public static void main(String[] args) { Direction direction = Direction.valueOf("EAST"); System.out.println(direction); } } Output : EAST
ordinal
Enum constants are properly indexed one after other . By using ordinal() method we can get the indexes
enum Direction { EAST("East"), WEST("West"), NORTH("North"), SOUTH("South"); String value; Direction(String value) { this.value = value; } } public class Demo { public static void main(String[] args) { Direction[] directions = Direction.values(); for (Direction direction: directions) { System.out.println(direction + " Index is : " + direction.ordinal()); } } } Output : EAST Index is : 0 WEST Index is : 1 NORTH Index is : 2 SOUTH Index is : 3
toString
From the above examples we have seen whenever we are printing the named constant its printing the constant name itself .
enum Direction { EAST, WEST, NORTH, SOUTH; } public class Demo { public static void main(String[] args) { Direction[] directions = Direction.values(); for (Direction direction: directions) { System.out.println(direction); } } } Output : EAST WEST NORTH SOUTH
But we can override toString method in the enum if we are having more business friendly names . As mentioned earlier enums by default extends Enum class ( which is having a toString method ) .
toString() method is overridden in java.lang.Enum class , which returns enum constant name . Hence whenever we try to print the enum constant it returns its name . Check the snap below :
Now we can override toString method of Enum class to provide more business friendly names .
enum Direction { EAST("East"), WEST("West"), NORTH("North"), SOUTH("South"); String value; Direction(String value) { this.value = value; } public String toString() { return value + " DIRECTION"; } } public class Demo { public static void main(String[] args) { Direction[] directions = Direction.values(); for (Direction direction: directions) { System.out.println(direction); } } } Output : East DIRECTION West DIRECTION North DIRECTION South DIRECTION
Hope you liked the article , if you want to add anything extra please comment below and write to me if i have gone wrong anywhere or you want to understand any other concept .