Introduction to Java Enum
Java enum is a special class in the Java programming language that represents group constants. It contains unchangeable variables such as variable final. When creating an enum class, we use the enum keyword rather than class or interface. When we use an enum in Java, we use a comma to separate the constants. Within our class, we also use enum.
Key Takeaways
- We are using an enum to create our own classes; we are using an enum keyword for creating our own enum type.
- To define the enum in Java, we use the enum keyword. It is a special class representing the constant group, like the final variable.
Overview of Java Enum
In Java, an enum is a special data type that enables variables to set predefined constants. The variable that we have defined is equal to the values that we have defined. Because it will be constants, we can say that the enum example compass directions. The enum-type fields are always in capital letters.
The enumeration in Java represents a group by incorporating named constants into the programming language. In Java, we use an enum when we know that all possible values will be available at the time of compilation. It is not necessary to keep the enum type constants fixed all of the time.
Java Enum Class
In Java, we define enum inside as well as outside of a class. In java, an enum is defined as a class type, so we do not need to instantiate the enum by using new; it will contain the same capabilities as per other classes. This feature makes enumeration useful. Like we give the constructor and also add the instance variables.
In the below example, we are defining the enum datatype inside the class as follows:
We are creating the class name as enum_class as follows:
Code:
public class enum_class {
enum Level {
LOWER,
MEDIUM,
HIGHER
}
public static void main(String[] args) {
Level mv = Level.MEDIUM;
System.out.println (mv);
}
}
Output:
In the below example, we are defining the enum datatype outside of the class as follows. We are creating the class name as enum_class as follows.
Code:
enum Color {
GREEN,
BLACK,
BLUE;
}
public class enum_class {
public static void main(String[] args)
{
Color col = Color.BLUE;
System.out.println (col);
}
}
Output:
Java Enum Switch Statement
We can also pass the enum data type by using a switch statement. The enum data type is used in the switch statement to check the values. The example below shows how an enum is used with a switch statement to define the level as follows.
Code:
enum lev {
LOWER,
MEDIUM,
HIGHER
}
public class enum_class {
public static void main(String[] args) {
lev mv = lev.MEDIUM;
switch (mv) {
case LOWER:
System.out.println ("level 1");
break;
case MEDIUM:
System.out.println ("level 2");
break;
case HIGHER:
System.out.println ("level 3");
break;
}
}
}
Output:
The below example shows a switch statement with an enum data type as follows. In the below example, we are giving day as an enum data type as follows.
Code:
import java.util.Scanner;
enum Day {S, M, TU, W, T, F, SA;}
public class enum_class {
Day d;
public enum_class(Day day) { this.d = day; }
public void dayIsLike()
{
switch (d) {
case M:
System.out.println ("Mon is second day.");
break;
case F:
System.out.println ("Fri is sixth day");
break;
case SA:
case S:
System.out.println ("Sun is weekend");
break;
default:
System.out.println ("Tue is third day.");
break;
}
}
public static void main(String[] args) {
{
String str = "M";
enum_class ec = new enum_class(Day.valueOf (str));
ec.dayIsLike ();
}
}
}
Output:
Methods
The enum will contain abstract and concrete methods. If the enum class contains abstract methods, then every instance of the enum class implements the same. The below example shows enum methods as follows.
The values method is used to return all the values which were present inside the enum. Order is important in enum methods; while using the ordinal method, each enum index is found in the array of index. The value of the method returns the enum constant, which was specified by using the existing string value.
Below example shows enum methods as follows:
Code:
enum col
{
WHITE,
RED,
BLUE;
}
public class enum_class
{
public static void main(String[] args)
{
col arr[] = col.values();
for (col col : arr)
{
System.out.println (col + "index"
+ col.ordinal());
}
System.out.println (col.valueOf ("RED"));
}
}
Output:
Constructors
Enum contained the constructor and was executed separately for every enum constant on which we are loading the enum class. We cannot create the objects of the enum explicitly, so we cannot invoke the constructor of the enum directly.
Below example shows the enum constructor as follows:
Code:
enum col {
PINK,
YELLOW,
GREEN;
private col()
{
System.out.println ("Con called: "
+ this.toString());
}
public void colorInfo()
{
System.out.println ("Uni Color");
}
}
public class enum_class {
public static void main(String[] args)
{
col cl = col.PINK;
System.out.println (cl);
cl.colorInfo ();
}
}
Output:
Java Enum Types
The java enum type is a special data type that enables the variable to set the constant, which was predefined. The variable is equal to the value which we have predefined. In Java, we define the type of enum by using the keyword as an enum. In the below example, we are defining the enum type of color as follows.
Example:
Code:
enum Col {
BLUE,
PINK,
BLACK
}
We are using the type of enum at any time to represent the fixed set of constants, including the natural enum types. The below example shows how we can define the types as follows.
In the below example, we are defining the constant set of numbers as follows:
Code:
public class enum_class {
enum number {
ONE,
TWO ,
THREE
}
public static void main(String[] args) {
number num = number.TWO;
System.out.println (num);
}
}
Output:
The below example shows java enum types. In the below example, we are defining the enum type of days as follows.
Code:
public class enum_class {
enum day {
MON,
TUE,
WED
}
public static void main(String[] args) {
day d = day.TUE;
System.out.println (d);
}
}
Output:
Importance
Enum basically inherits from the enum class, so it won’t inherit any other classes. Below is the importance..
- It improves the type of safety.
- We are using it easily in a switch.
- Enum in Java is traversed.
- It contains the constructors, methods, and fields.
- It implements the interfaces but will not extend any class; it will internally extend the enum class.
We are using an enum to create our own data types. The enum data type we use to define enum in Java.
Below are the important characteristics as follows:
- Constant of the enum is not overridden.
- Enum does not support the objects creation.
- Enum does not extend the other classes.
- Enum implements class-like interfaces.
- We are using an enum in a method.
- We are using an enum in a constructor.
Conclusion
In Java, an enum is a special data type that enables variables to set predefined constants. The variable that we have defined is equal to the values that we have defined. It is a special class used in the Java programming language to represent group constants. It contains unchangeable variables like variables as final.
Recommended Articles
This is a guide to Java Enum. Here we discuss the introduction, java enum class, switch statement, methods, constructors, and types. You may also have a look at the following articles to learn more –
41 Online Courses | 29 Hands-on Projects | 305+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses