English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, we will learn about enums in Java. We will learn to create and use enums and enum classes with the help of examples.
In Java, an enum is a type that has a fixed set of possible values. We use the enum keyword to declare an enum. For example,
enum Size { SMALL, MEDIUM, LARGE, EXTRALARGE }
Here, we create an enum named Size.
The values inside the curly braces are called enum values (constants). These are the unique values that can be retained by the enum type.
Note:Enum constants are usually represented in uppercase.
Let's take a simple example.
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 result
SMALL MEDIUM
It can be seen from the above example that we access constant values using enum names.
Similarly, we can create variables of enum types. For example,
Size pizzaSize;
Here, pizzaSize is a variable of type Size. It can only be assigned4values.
pizzaSize = Size.SMALL; pizzaSize = Size.MEDIUM; pizzaSize = Size.LARGE; pizzaSize = Size.EXTRALARGE;
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 pizza."); break; case MEDIUM: System.out.println("I ordered a medium 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 result
I ordered a medium pizza.
In the above program, we created an enum type Size. Then, we declared a variable pizzaSize of type Size.
The variable pizzaSize can only be assigned4values (SMALL, MEDIUM, LARGE, EXTRALARGE).
The variable pizzaSize is assigned the MEDIUM constant. Based on this, one case of the switch case statement will be printed.
In Java, enum types are considered a special type of class. It is in Java 5Introduced in the release.
Enum classes can include methods and fields like regular classes.
enum Size { constant1, constant;2, ..., constantN; //Methods and fields }
Enum constants are always default to public static final.
When creating an enum class, the compiler will also create instances (objects) for each enum constant.
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 the method getSize() using the object SMALL System.out.println("The size of the pizza is ", + Size.SMALL.getSize()); } }
Output result
The size of the pizza is small
In the above example, we created an enum class Size. It has four constants SMALL, MEDIUM, LARGE, and EXTRALARGE.
Since Size is an enum class, the compiler will automatically create an instance for each enum constant.
Inside the main() method, we have used the instance SMALL to call the getSize() method.
Like regular classes, enum classes can also contain constructors. For more information, please visitJava Enum Constructor.
There are some predefined methods in the enum class that can be used at any time.
The ordinal() method returns the position of the enum constant. For example,
ordinal(SMALL) //returns 0
The compareTo() method compares enum constants based on their ordinal values. For example,
Size.SMALL.compareTo(Size.MEDIUM) //returns ordinal(SMALL) - ordinal(MEDIUM)
The toString() method returns the string representation of the enum constant. For example,
SMALL.toString() //returns "SMALL"
The name() method returns the defining name of the enum constant in string form. The value returned by the name() method is final. For example,
name(SMALL) //returns "SMALL"
The valueOf() method takes a string and returns the enum constant with the same string name. For example,
Size.valueOf("SMALL") //returns the constant SMALL
The values() method returns an array of enum types containing all enum constants. For example,
Size[] enumArray = Size.values();
In Java, enums were introduced to replace the use of int constants.
Suppose we used a collection 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, if we print the constants, the problem arises. This is because it only prints numbers, which may not be helpful.
Therefore, we can simply use enums instead of using int constants. For example,
enum Size { SMALL, MEDIUM, LARGE, EXTRALARGE }
This makes our code more intuitive.
Similarly, enums provide compile-time type safety.
If we declare a variable of the Size type (as shown in the above example), we can ensure that the variable will hold one of the four values. If we try to pass a value outside of these four, the compiler will throw an error.