Enumerated data types (or enums for short) are newly introduced in Java 5.0. Enums will let us choose a value from a pre-defined list of values. Each value in this pre-defined list are called “enums”. Using enums we can restrict the values thus by reducing bugs in our code.
Suppose, if we are developing payroll application, we need to have certain employee types. For example, permanent employee, contract employee, full time employee and part-time employee etc. In order to provide these employee types, we can create an enum with these pre-defined employee types as shown below:
enum EmployeeType { PERMANENT, CONTRACT, FULLTIME, PARTTIME }
Once we have enum type in place, we can choose the required enum type as shown below.
class Employee {
EmployeeType employeeType;
}
class EmployeeTest {
public static void main(String… args) {
new Employee().employeeType = EmployeeType.PERMANENT;
}
}
It is a good practise to keep enum constants in upper case letters though it is not required. We can declare enum types in separate classes. We can also use enums as class members, however we cannot declare enums in a method. Another important thing to note that, each of the enum constants are actually instances of enum types. In our example, each of the enum types PERMANENT, CONTRACT, FULLTIME and PARTTIME are instances of type EmployeeType.
Define methods, variables and constructors in Enums
For example, if we want to maintain salary information for the each of the employee types in our previous example, enums constructors are really useful in this kind of scenarios. The following code example demonstrates how salary information can be assigned to an enum type.
public enum EmployeeType {
PERMANENT(1000), CONTRACT(1200), FULLTIME(1500), PARTTIME(500);
private int salary;
public int getSalary() {
return salary;
}
EmployeeType(int salary) {
this.salary = salary;
}
}
As we noticed, we have an instance variable called “salary” in the enum declaration with getter method getSalary(). We also have enum constructor which takes salary and assigns the value to the instance variable salary. We are making use of this constructor at the beginning and creating our employee types by passing the salary information.
Now, here is our Employee class which makes use of the just created enum EmployeeType. This is pretty simple class.
public class Employee {
EmployeeType employeeType;
}
The following code example creates an employee instance and gets salary information based on the enum type provided.
public class EnumTest {
public static void main(String... args) {
Employee permanentEmployee = new Employee();
permanentEmployee.employeeType = EmployeeType.PERMANENT;
System.out.println(permanentEmployee.employeeType.getSalary());
}
}
Output
1000
As we see, we are able to fetch salary information based on the enum type provided. As a matter fact, we can have as much relevant information as possible kept in the enums for usage at later point.
Iterating enum values
If we want to iterate through all the types of an enum along with values we can use static method values() which returns an array of enum values in the order of declaration. The following code snippet iterates through enum values and prints enum values.
for (EmployeeType empType : EmployeeType.values()) {
System.out.println(empType +" : "+ empType.getSalary());
}
Output
PERMANENT : 1000
CONTRACT : 1200
FULLTIME : 1500
PARTTIME : 500
- Top 45 Fresher Java Interview Questions - March 9, 2023
- 25 Free Practice Questions – GCP Certified Professional Cloud Architect - December 3, 2021
- 30 Free Questions – Google Cloud Certified Digital Leader Certification Exam - November 24, 2021
- 4 Types of Google Cloud Support Options for You - November 23, 2021
- APACHE STORM (2.2.0) – A Complete Guide - November 22, 2021
- Data Mining Vs Big Data – Find out the Best Differences - November 18, 2021
- Understanding MapReduce in Hadoop – Know how to get started - November 15, 2021
- What is Data Visualization? - October 22, 2021