Java Flags Enum
In a programming language, enumerations represent a group of named constants.For instance; the four suits in a deck of playing cards could be the enumerators Club, Diamond, Heart, and Spade, all members of the enumerated type Suit. Additional examples include enumerated natural types, such as planets, days of the week, colours, and directions.
Enums are utilized when we know all potential qualities at gather time, for example, decisions on a menu, adjusting modes, order line banners, and so on.The set of constants in an enum type need not be constant at all times.
A class type is a Java enumeration. An enum has the same capabilities as other classes, so we don't need to instantiate it with new ones.This reality makes Java list an exceptionally incredible asset.You can give them a constructor, add instance variables and methods, and even set up interfaces like you can with classes.
In contrast to classes, enumerations cannot be extended (i.e., become a superclass) or inherited from other classes.
Variables, methods, and constructors can all be added to it in Java.Enum is primarily used to define our data types (known as enumerated data types).
Enum statement in Java:
Enum declarations can beinside or outsidea class but not inside a method.
// A simple enum example where enum is declared
// outside any class (Note enum keyword instead of
// class keyword)
enumColor {
RED,
GREEN,
BLUE; }
public class Test {
// Driver method
public static void main(String[] args) {
Color c1 = Color.RED;
System.out.println(c1);
} }
Output:
RED
// enum declaration inside a class.
public class Test {
enumColor {
RED,
GREEN,
BLUE;
}
// Driver method
public static void main(String[] args)
{
Color c1 = Color.RED;
System.out.println(c1);
}
}
Output:
RED
The first line of the enumeration is a list of constants, followed by methods, variables, and constructors.
According to Java naming conventions, constants must be namedin uppercase only.
Every enum constant represents an object of type enum.
Enum type can be passed as an argument to switch statements.
// A Java program to demonstrate working on enum
// in switch case (Filename Test. Java)
import java.util.Scanner;
// An Enum class
enum Day {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY; }
public class Test {
Day day;
// Constructor
public Test(Day day) { this.day = day; }
// Prints a line about Day using switch
public void dayIsLike() {
switch (day) {
case MONDAY:
System.out.println("Mondays are bad.");
break;
case FRIDAY:
System.out.println("Fridays are better.");
break;
case SATURDAY:
case SUNDAY:
System.out.println("Weekends are best.");
break;
default:
System.out.println("Midweek days are so-so.");
Break; } }
// Driver method
public static void main(String[] args)
{
String str = "MONDAY";
Test t1 = new Test(Day.valueOf(str));
t1.dayIsLike();
}
}
Output:
Mondays are bad
Each enum constant is always implicitly static public final. It's static, so you can access it byenum name. It's final, so you can't create sub-enums.
You can declare a main() method inside an enum. So you can call the enum directly from the command prompt.
// A Java program to demonstrate that we can have
// main() inside enum class.
enumColor {
RED,
GREEN,
BLUE;
public static void main(String[] args)
{
Color c1 = Color.RED;
System.out.println(c1); } }
Output:
RED
Enums and Inheritance
All enum types implicitly extend the java. Lang.Enum class A Java class can only extend one parent, so an enum cannot extend anything else. The toString() method is overridden in java. Lang.Enum class to return the enum constant name. An enum can implement many interfaces.
values(), ordinal() and valueOf() methods:
These methods are in java. Lang Enum You can use the values() methodto return any value present in the enum. The order of the list is important. You can use the ordinal() method to find any enum constant index, similar to array indices. If any, the valueOf() method returns the enum constant of the specified string value.
Enumerations and constructors:
An enum can execute a constructor separately for each enum constant when theenum class is loaded.
Enum objects cannot be created explicitly, so the Enum constructor cannot be called directly.
Enumerations and methods:
An enum can contain both concreteand abstract methods. If an enum class has an abstract method, each instance of the enum class must implement it