How to use Java enums to create constants in your code
With Java enums, you can create variables with fixed values. These are written in capital letters and cannot be changed. They are used to improve the logic and readability of your code and can help you to create clean code.
What are Java enums?
In Java, enums refers to a special data type that can be used within conventional classes but differ from conventional classes in one key aspect. An enum is a type of variable that has specifically defined, fixed and therefore restricted value options. Java enums are constants that prevent unwanted values from being used within the code. This ensures the structure of the code is logical and increases readability. The term “enum” comes from the word “enumeration”.
- 99.9% uptime
- PHP 8.3 with JIT compiler
- SSL, DDoS protection, and backups
What is the syntax for Java enums?
The syntax for Java enums is always structured the same and looks like this:
enum NameOfTheClass {
VALUE1,
VALUE2,
VALUE3
}
javaSo if you want to define an enum class, start with the keyword enum
, which you use instead of class
or interface
. Then name the special class and place the desired permitted constants in the curly brackets. The constants are always written in capital letters and separated from each other by commas.
How to create a class with enum
One way you can use Java enums is to create a list containing the months of the year. Such a list would only contain twelve possible values. If a variable of the type Months
is needed at a later point in time, only the defined constants are permitted. Here’s how to write the code:
enum Months {
JANUARY,
FEBRUARY,
MARCH,
APRIL,
MAY,
JUNE,
JULY,
AUGUST,
SEPTEMBER,
OCTOBER,
NOVEMBER,
DECEMBER
}
javaTo declare a variable of the type Months
, assign it a value and use the Java command System.out.println
to display it. This looks like this in the code:
public class Main {
public static void main(String[] args) {
Months month = Months.APRIL;
System.out.println(month);
}
}
javaHow to use Java enums within a class
You can also use Java enums within a class. For the example above, it would look like this:
public class Main {
enum Months {
JANUARY,
FEBRUARY,
MARCH,
APRIL,
MAY,
JUNE,
JULY,
AUGUST,
SEPTEMBER,
OCTOBER,
NOVEMBER,
DECEMBER
}
public static void main(String[] args) {
Months month = Months.APRIL;
System.out.println(month);
}
}
javaHow to use the values()
method to query all constants
If you want to display all available values, you can use the values()
method. This outputs constants in the form of an array. Here’s how to do it with our example from above:
enum Months {
JANUARY,
FEBRUARY,
MARCH,
APRIL,
MAY,
JUNE,
JULY,
AUGUST,
SEPTEMBER,
OCTOBER,
NOVEMBER,
DECEMBER
}
public class Main {
public static void main(String[] args) {
for (Months month : Months.values()) {
System.out.println(month);
}
}
}
javaHow to use Java enums with switch()
You can also combine Java enums with other methods. Here’s what it looks like when it’s combined with switch()
:
enum Months {
JANUARY,
FEBRUARY,
MARCH,
APRIL,
MAY,
JUNE,
JULY,
AUGUST,
SEPTEMBER,
OCTOBER,
NOVEMBER,
DECEMBER
}
public class Main {
public static void main(String[] args) {
Months month = Months.APRIL;
switch (month) {
case APRIL:
System.out.println("The outdoor pool is closed.");
break;
case MAY:
System.out.println("The outdoor pool is closed.");
break;
case JUNE:
System.out.println("The outdoor pool eagerly awaits your visit!");
break;
// You can now try this out for all months.
}
}
}
java