Introduction
In a computer language, enumerations stand in for a collection of named constants. For instance, the four suits in a deck of playing cards could be represented by the counters Club, Diamond, Heart, and Spade from the listed suit.
What is Enumeration in Java?
Enum is a unique data type that was first used in Java 5 and consists of named preset values that are separated by commas. These named values are either enumerators, enum instances, or elements. Enum values should always be represented in uppercase letters because they are constant.
When you require a fixed set of predefined constant values that are known at build time, you can use the Enum type. Examples include the seasons and days of the week.
The following properties make an enumeration a "special" class:-
- enumeration constants cannot be overridden
- enum does not support object creation
- enum cannot extend other classes
- enum can implement interfaces as classes
Syntax:
In Java, the keyword "enum" is used to define an Enum type, rather than "class" or "interface". Below is the syntax.
enum Variable Name { VALUE_1, VALUE_2, VALUE_3, … }
Our Learners Also Read: Explain Serialization in Java
Example of Java Enum
- class EnumExample1{
- //defining the enum inside the class
- public enum Season { WINTER, SPRING, SUMMER, FALL }
- //main method
- public static void main(String[] args) {
- //traversing the enum
- for (Season s : Season.values())
- System.out.println(s);
- }}
Output:
WINTER
SPRING
SUMMER
FALL
Why Do We Need an Enumeration?
Java supports two types of data types:-
- User-defined data types
- Built-in data types – int, float, double, char, etc.
Sometimes the built-in data types are not enough. Suppose we have data of different data types that need to be stored in one variable. In such a situation, the built-in data types will not meet the need. Because of this, user-defined data types—among which Java's enum is one—are necessary.
How to Define a Java Enum?
Enum in Java can be defined outside or within a class.
Syntax: enum Name{constants}
The following example will teach us how to define an enum in Java.
Code:
package definition;
enum Car{
MARUTI, HONDA, TESLA;
}
public classExample{
public static void main(String [] args) {
Carc1= Car.HONDA;
System.out.println(c1);
}
}
How to use Enum in Java?
The primary use of an enumeration is for comparison, similar to our example of a simple validation enumeration. You can use it to compare users' permission levels with their respective permissions. Comparison operations can be used if blocks, switch statements, functions, loops, etc.
Let's look at examples of these uses and see how to make these comparisons.
String[] currUser = {"John Smith", "Write"};
if(Auth.ADMIN.permission == currUser[1]){
System.out.println(true);
}
Written in the main class, this setup checks the permission level and permissions of the current user and then performs the specified action. This could verify the user's authorization to perform certain actions within the software, such as editing information.
Enum Constructors, Member Variables, and Methods
Enums can have constructors, member variables, methods, and interface implementation much like Java classes. The fact that Enum constants are public, static, and final is the only distinction. Additionally, you can explicitly extend any other class or construct it with the "new" keyword.
Abstract methods in the enumeration:
You can also have abstract methods inside an Enum type. If there is an abstract method, each constant enumeration value should implement it. This is useful when implementing a different method for each constant value.
Interface with Enum:
An interface can be implemented with an Enum type, just like abstract methods.
EnumSet:
An advanced Java implementation that works with Enum types is this abstract class. It is a more effective, superior, and type-safe replacement for the HashSet implementation because it is a component of the Java Collections Framework.
This enumeration set's elements must all only contain instances of the same enumeration type.
This is not synchronized and does not permit Null elements, so keep that in mind. It produces a NullPointerException if you attempt to insert a Null element.
numMap:
Like EnumSet, this is a technical implementation of Java Map to be used only with the Enum type.
The type of each key in an EnumMap must be the same. Null keys are prohibited. The system will throw a NullPointerException if you attempt to insert a Null key. "Null" values are permitted, nevertheless.
Conclusion
Enum is a special data type that contains a comma-separated set of predefined named values. These named values are called elements, enumerators, or enumeration instances.
When a fixed collection of predetermined constant values is required, the enumeration type can be utilized during software compilation. Since these are constant values, you should declare them in UPPERCASE LETTERS.