Java Enumeration
In a computer language, enumerations express a set of named constants. For instance, the four suits in a deck of playing cards could be represented by the enumerators Club, Diamond, Heart, and Spade, members of the enumerated type Suit. The enumerated natural kinds are another example (like the galaxies, weeks in a month, colours, etc.).
When we know every potential value at compilation time, such as for menu options, rounding modes, command-line flags, etc., enums are employed. An enum type's constants need not always remain consistent over time.
Enumerations in Java are class types. Enums have the same features as other classes even though we don't need to instantiate them using new ones. Java enumeration is an extremely potent tool as a result of this feature. They can have constructors, instance variables, and methods added, just like classes, and you can even create interfaces.
Unlike classes, enumerations cannot inherit from other classes or be expanded, which is something to keep in mind (i.e. become superclass).
Enums are expressed using the enum data type in Java. Enums in Java are more capable than those in C/C++. Variables, methods, and constructors can all be added in Java. Enum's primary goal is to allow us to create custom data types (Enumerated Data Types).
Java declaration of an enum
Enum declarations are permitted both inside and outside of classes but not inside of methods.
// An example of a simple enum when the enum is declared
// not in a class (Note that enum is
// the term here instead of class.)
enum Days {
Sun,
Mon,
Tue;
}
public class Kamal {
// method of the Driver
public static void main(String[] args)
{
Days d1 = Days.Sun;
System.out.println(d1);
}
}
Output:

// inside of a class, an enum declaration
public class Test1 {
enum Days {
Sun,
Mon,
Tue;
}
// method of the driver
public static void main(String[] args)
{
Days d1 = Days.Sun;
System.out.println(d1);
}
}
Output:

- Constants should appear on the first line of the enum, followed by a list of variables, constructors, and other items.
- According to Java naming conventions, it is recommended that we name constant with all capital letters.
Important Points to Remember
- Every enum has a Class implementation on the inside.
/* in the internal enum Days is changed into a class of Days.
{
public static final Day Sun = new Day();
public static final Day Mon = new Day();
public static final Day Tue = new Day();
}*/
- An object of the type enum is represented by each enum constant.
- Switch statements accept arguments of any type, including enum types.
// a Java programme that shows how to interact
// with enums in switch cases (Filename Test3. Java)
import java.util.Scanner;
// An Enum class
enum Days {
SUN,
MON,
TUE,
WED,
THU,
FRI,
SAT;
}
// a class of drivers that includes an object for "days" and
// the main().
public class Test3 {
Days days;
// Constructor
public Test(Days days) { this.days = days; }
// the switch is used to print a sentence regarding Days.
public void daysIsLike()
{
switch (days) {
case MON:
System.out.println("Mon are very boring.");
break;
case FRI:
System.out.println("Fri are the best.");
break;
case SAT:
case SUN:
System.out.println("Weekends are the best.");
break;
default:
System.out.println("Midweek days are so-so days.");
break;
}
}
// method of the driver
public static void main(String[] args)
{
String str = "MON";
Test3 t1 = new Test3(Days.valueOf(str));
t1.daysIsLike();
}
}
Output:

- All enum constants are inherently static public final. The enum Name can be used to access it because it is static. We cannot make child enums because it is final.
- The enum can contain a declaration for the main() method. So, using the Command Prompt, we can call enum.
// A Java application to show that main() can be
// contained within an enum class.
enum Days {
SUN,
MON,
TUE;
// method of the driver
public static void main(String[] args)
{
Days d1 = Days.SUN;
System.out.println(d1);
}
}
Output:

enum and the constructor:
- When the enum class is loaded, enums are allowed to have constructors, which are executed individually for each enum constant.
- Enum constructors cannot be called directly since we cannot explicitly create enum objects.
enum and the methods:
- enum can contain both concrete methods and abstract methods. If an enum class has an abstract method, then each instance of the enum class must implement it.
// Enums can have concrete methods and
// constructors, as shown by a Java programme.
// an enum (Note that enum is used in instead of class.)
enum Days {
Sun,
Mon,
Tue;
private Days()
{
System.out.println("Constructor called for : "
+ this.toString());
}
public void daysInfo()
{
System.out.println("Universal Days");
}
}
public class Test4 {
// method of the driver
public static void main(String[] args)
{
Days d1 = Days.Sun;
System.out.println(d1);
c1.daysInfo();
}
}
Output:
