enum in Java : the only article you need!

enums are constants. You define these constants so that the programmers can adhere to this rule set by you.

Take for example -
We are writing a program where based on the day of the week a programmer needs to take some action. In this case we know that there could be 7 days in a week - Monday to Sunday.

So we will create an enum named Days and assign 7 values from Monday to Sunday so that when programmers write the logic, they can only use these days which are assigned in the enum.

So you use enum types any time you need to represent a fixed set of constants.

One more example -
Say that you have a requirement where there could be 3 types of vehicles- fuel type, gas type, and electric type.
In this case we know all the possible values for vehicles before compile time, so we will create an enum with these possible values so that the programmers who build the application follow the rule set by us in the form of enum.

See, it is not mandatory to create an enum, but we use enum to follow good coding practices, and to rule out any issues in the system.

Enums are used to create our own data type like classes.
Enums in Java is much more powerful than C/C++.

Few points to know about Enums :

  1. We can define enum inside or outside the class.
  2. Enums cannot be defined inside methods.
  3. Enum cannot inherit any other class since it already inherits Enum class.
  4. Enums can implement interfaces.
  5. Enums can be traversed and used in switch case
  6. Enums can have fields, constructors and methods
  7. We can have main method inside an enum
  8. Every enum constants are public static final.
  9. java compiler internally adds values(), valueOf(), and ordinal() method.
  10. enums can contain concrete and abstract methods both.
  11. The first line inside the enum should be a list of constants.
  12. enum type can be passed as an argument to switch statements

This is how we define an enum :

enum Color {
YELLOW,
RED,
WHITE;
}

Usage of ordinal(), values(), valueOf():

import java.util.*;public class Main {
public static void main(String[] args)
{
// Calling values()
Color arr[] = Color.values();

// enum with loop
for (Color col : arr) {
// Calling ordinal() to find index
// of color.
System.out.println(col + " at index "
+ col.ordinal());

//printing the value of enum constant
System.out.println(col.value + " at index "
+ col.ordinal());
System.out.println();
}

// Using valueOf(). Returns an object of
// Color with given constant.
System.out.println(Color.valueOf("RED"));
}
}
enum Color {
YELLOW(1),
RED(10),
WHITE(15);

int value;
Color(int value) {
this.value = value;
}
}
Output:YELLOW at index 0
1 at index 0

RED at index 1
10 at index 1

WHITE at index 2
15 at index 2

RED

ordinal() returns the index of the enumeration constant (its position in its enum declaration, where the initial constant is assigned an index of zero). Similar to indexes in arrays.

valueOf() will return the value for the enum constant like 15 for WHITE

values() will return an array containing all the enum contsants

One more use case : What if we wanted the enum constant based on value ?

System.out.println(Color.valueOf("WHITE")); outputs 15

Like you wanted an output WHITE based on 15.

Implementation :

We will create a switch case block like below to get the enum constant based on the values.

enum Color {
YELLOW(1),
RED(10),
WHITE(15);

int value;
Color(int value) {
this.value = value;
}
public static Color valueOf(int value) {
switch (value) {
case 1:
return Color.YELLOW;
case 10:
return Color.RED;
case 15:
return Color.WHITE;
default:
return null;
}
}
}

To get the output :

System.out.println(Color.valueOf(15)); outputs WHITE

That’s it. Feel free to write any questions or leave a note to connect. Please follow for more such tech articles around full stack development.

Happy Learning! Cheers.:)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store