Enum in Java
Enum in Java
Enum Example:
One thing to keep in mind is that, unlike classes, enumerations neither inherit
other classes nor can get extended(i.e become superclass). We can also add
variables, methods, and constructors to it. The main objective of an enum is to
define our own data types(Enumerated Data Types).
Enum declaration can be done outside a class or inside a class but not inside
a method.
Java
// A simple enum example where enum is declared
// outside any class (Note enum keyword instead of
// class keyword)
enum Color {
RED,
GREEN,
BLUE;
}
Output
RED
// Driver method
public static void main(String[] args) {
Color c1 = Color.RED;
System.out.println(c1);
}
}
Output
RED
● The first line inside the enum should be a list of constants and then
other things like methods, variables, and constructors.
● According to Java naming conventions, it is recommended that we
name constant with all capital letters
Java
// A Java program to demonstrate working on enum
// in a switch case (Filename Test.java)
import java.util.Scanner;
// An Enum class
enum Day {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY;
}
// Constructor
public Test(Day day) {
this.day = day;
}
// Driver method
public static void main(String[] args) {
String str = "MONDAY";
Test t1 = new Test(Day.valueOf(str));
t1.dayIsLike();
}
}
Output
Mondays are bad.
Enums can have a main function, allowing them to be invoked directly from the
command line.
// Driver method
public static void main(String[] args) {
Color c1 = Color.RED;
System.out.println(c1);
}
}
Output
RED
The above program will not run on online compiler. To run this program, save
it as Color.java and use the java command to compile and execute.
We can iterate over the Enum using the values() method, which returns an
array of enum constants.
Java
// Java Program to Print all the values
// inside the enum using for loop
import java.io.*;
// Enum Declared
enum Color {
RED,
GREEN,
BLUE;
}
// Driver Class
class GFG {
// Main Function
public static void main(String[] args) {
// Iterating over all the values in
// enum using for loop
for (Color var_1 : Color.values()) {
System.out.println(var_1);
}
}
}
Output
RED
GREEN
BLUE
Java
// Java Program to implement
// Enum in a Switch Statement
import java.io.*;
// Driver Class
class GFG {
// Enum Declared
enum Color {
RED,
GREEN,
BLUE,
YELLOW;
}
// Main Function
public static void main(String[] args) {
Color var_1 = Color.YELLOW;
Output
Other color observed
Enums can have both concrete and abstract methods. If an enum class has
an abstract method, each enum constant must implement it.
Java
// Java program to demonstrate that enums can have
// constructor and concrete methods.
Output
Constructor called for : RED
Constructor called for : GREEN
Constructor called for : BLUE
RED
Universal Color
Note: While the set of constants in a Java enum is fixed at runtime and
cannot be changed dynamically, you can modify the source code of the enum
to add or remove constants and then recompile the application to reflect these
changes.
When combined with classes, enum can serve as powerful tool for organising
program.
Java
/*package whatever //do not write package name here */
import java.io.*;
enum Day {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
}
Output
Mondays are tough
Wednesday are okay
Fridays are exciting
Saturdays are relaxing
Sunday are for rest
The EnumTest class in above code is created with member of type Day. It has
constructor which takes Day enum as an argument and assigns it.
The class has method tellItLikeItIs(), which prints message based on value of
day.
The main method includes objects of EnumTest using different Day enum
values and calling tellItLikeItIs() method on each.
NOTE: In the main method new is used when creating instances of EnumTest
because EnumTest is a regular Java class not enum itself. So, the new
keyword is used to create new instance of EnumTest class, and passing enum
value to its constructor.