In this article, you will learn-
Java enums
In this tutorial, we will find out about enums in Java. We will learn to make and use enums and enum classes with the help of examples.
In Java, an enum (short for enumeration) is a sort that has a fixed arrangement of steady qualities. We use the enum catchphrase to pronounce enums. For instance,
enum Size {
SMALL, MEDIUM, LARGE, EXTRALARGE
}
Here, we have created an enum named Size. It contains fixed values SMALL, MEDIUM, LARGE, and EXTRALARGE.
These values inside the braces are called enum constants (values).
Note: The enum constants are usually represented in uppercase.
Example 1: Java Enum
enum Size {
SMALL, MEDIUM, LARGE, EXTRALARGE
}
class Main {
public static void main(String[] args) {
System.out.println(Size.SMALL);
System.out.println(Size.MEDIUM);
}
}
Output
SMALL
MEDIUM
As should be obvious from the above example, we use the enum name to access consistent qualities.
Additionally, we can make variables of enum types. For instance,
Size pizzaSize;
Here, pizzaSize is a variable of the Size type. It can only be assigned with 4 values.
pizzaSize = Size.SMALL;
pizzaSize = Size.MEDIUM;
pizzaSize = Size.LARGE;
pizzaSize = Size.EXTRALARGE;
Example 2: Java Enum with the switch statement
enum Size {
SMALL, MEDIUM, LARGE, EXTRALARGE
}
class Test {
Size pizzaSize;
public Test(Size pizzaSize) {
this.pizzaSize = pizzaSize;
}
public void orderPizza() {
switch(pizzaSize) {
case SMALL:
System.out.println("I ordered a small size pizza.");
break;
case MEDIUM:
System.out.println("I ordered a medium size pizza.");
break;
default:
System.out.println("I don't know which one to order.");
break;
}
}
}
class Main {
public static void main(String[] args) {
Test t1 = new Test(Size.MEDIUM);
t1.orderPizza();
}
}
Output
I ordered a medium size pizza.
In the above program, we have created an enum type Size. We then declared a variable pizzaSize of the Size type.
Here, the variable pizzaSize can only be assigned with 4 values (SMALL, MEDIUM, LARGE, EXTRALARGE).
Notice the statement,
Test t1 = new Test(Size.MEDIUM);
It will call the Test() constructor inside the Test class. Now, the variable pizzaSize is assigned with the MEDIUM constant.
Based on the value, one of the cases of the switch case statement is executed.
Enum Class in Java
In Java, enum types are considered to be a special type of class. It was presented with the arrival of Java 5.
An enum class can incorporate strategies and fields simply like regular classes.
enum Size {
constant1, constant2, …, constantN;
// methods and fields
}
At the point when we make an enum class, the compiler will make occasions (objects) of each enum constants. Likewise, all enum constant is the consistently public static final by default.
Example 3: Java Enum Class
enum Size{
SMALL, MEDIUM, LARGE, EXTRALARGE;
public String getSize() {
// this will refer to the object SMALL
switch(this) {
case SMALL:
return "small";
case MEDIUM:
return "medium";
case LARGE:
return "large";
case EXTRALARGE:
return "extra large";
default:
return null;
}
}
public static void main(String[] args) {
// call getSize()
// using the object SMALL
System.out.println("The size of the pizza is " + Size.SMALL.getSize());
}
}
Output
The size of the pizza is small
In the above example, we have created an enum class Size. It has four constants SMALL, MEDIUM, LARGE and EXTRALARGE.
Since Size is an enum class, the compiler automatically creates instances for each enum constants.
Here inside the main() method, we have used the instance SMALL to call the getSize() method.
Note: Like regular classes, an enum class also may include constructors.
Methods of Java Enum Class
There are some predefined methods in enum classes that are readily available for use.
1. Java Enum ordinal()
The ordinal() method returns the position of an enum constant. For example,
ordinal(SMALL)
// returns 0
2. Enum compareTo()
The compareTo() method compares the enum constants based on their ordinal value. For example,
Size.SMALL.compareTo(Size.MEDIUM)
// returns ordinal(SMALL) - ordinal(MEDIUM)
3. Enum toString()
The toString() method returns the string representation of the enum constants. For example,
SMALL.toString()
// returns "SMALL"
4. Enum name()
The name() method returns the defined name of an enum constant in string form. The returned value from the name() method is final. For example,
name(SMALL)
// returns "SMALL"
5. Java Enum valueOf()
The valueOf() method takes a string and returns an enum constant having the same string name. For example,
Size.valueOf("SMALL")
// returns constant SMALL.
6. Enum values()
The values() method returns an array of enum type containing all the enum constants. For example,
Size[] enumArray = Size.value();
Why Java Enums?
In Java, enum was introduced to replace the use of int constants.
Assume we have used an assortment of int constants.
class Size {
public final static int SMALL = 1;
public final static int MEDIUM = 2;
public final static int LARGE = 3;
public final static int EXTRALARGE = 4;
}
Here, the issue emerges if we print the constants. It is on the grounds that lone the number is printed which probably won’t be useful.
Along these lines, rather than using int constants, we can essentially use enums. For instance,
enum Size {
SMALL, MEDIUM, LARGE, EXTRALARGE
}
This makes our code more intuitive.
Additionally, enum gives incorporate compile-time type safety.
If we announce a variable of the Size type. For instance,
Size size;
Here, it is ensured that the variable will hold one of the four qualities. Presently, If we attempt to pass values other than those four qualities, the compiler will produce a mistake.
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.