×

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


Related Topics

Minimum Window Subsequence in Java

In this article, you will be very well acknowledged about the minimum window subsequence, what is the approach and how it is implemented. The example program is also executed and...

4 minutes read.

Alice and Bob Problem Java

Alice and Bob were two friends who liked to play games. Both together found a game, which has the description below. The game begins with an integer num, used to create...

2 minutes read.

How to Convert String to boolean in Java

How to Convert String to boolean in Java There are two methods to convert String to boolean: Using parseBoolean(string) method Using valueOf(string) method If the string contains "True," "true," or "TRUE,"...

3 minutes read.

How to Run Applet Program in Java

An applet is a new type of program which is put in a webpage. By inserting applet into a webpage dynamic content can be created. Characteristics of an Applet The applet is...

11 minutes read.

How to Convert String to Object in Java

How to Convert String to Object in Java The Object is the super class of all classes. So you can assign a string to Object directly. There are two methods to...

3 minutes read.

Hidden classes in Java

There specifically are some APIs available in the market that generally is harmful to be used in our programs specifically literally, and until JDK 15, there, for all intents and...

4 minutes read.

Java String startsWith() method

Java String startsWith() method checks whether current String starts with given prefix or not . Syntax: public boolean startsWith(String prefix) public boolean startsWith(String prefix, int offset) Parameters: prefix : It is sequence of character Returns: It returns...

2 minutes read.

How to Split String by Comma in Java

strsplit() technique permits you to break a string given the explicit Java string delimiter. The Java string split property is frequently a space or a comma(,) that you want to...

7 minutes read.

Unicode System in Java

In this tutorial, we will understand the meaning and emergence of the Unicode system in java. The Unicode system is one of the important and useful features in the world...

6 minutes read.

Parallel Arrays Sort in Java

Sorting is a technique of arranging a sequence of numbers in ascending order, that is, the first number being lowest and gradually incrementing the numbers such that the last number...

7 minutes read.

Java Programming certification

The most common technology utilized in the creation of applications is Java. People and businesses like it because it turns original ideas into useful software solutions. A Java programming certification can...

6 minutes read.

Java Integer compare() method

The compare() method of Integer class compares the two specified int values. Syntax public static int compare(int x, int y) Parameters The parameters ‘x’ and ‘y’ represent the first and second int values to...

2 minutes read.

Sort Dates in Java

The Collection class's sort() method, which is a component of the Comparator mechanism, arranges the data in decreasing order. The Comparator interface can be used to accomplish the goal while...

4 minutes read.

Palindrome number program in Java

Write java program to check if a number is palindrome in Java? A number that does not change on reversing is called a palindrome number. In other words, when reversing the...

3 minutes read.

Java Math toRadians() Method

The toRadians() method of Java Math class converts an angle measured in degrees to an approximately equivalent angle measured in radian. Syntax: public static double toRadians (double angdeg) Parameters The parameter ‘angdeg’ represents an...

2 minutes read.

How to add Elements in Array in Java

Consider an array of the size n, in the given size we must add the elements in the given array. In this tutorial we are preferred to learn how to add...

3 minutes read.

How to resolve Illegal state exceptions in Java

What is IllegalStateException? When a method is called at the incorrect time, a runtime exception called an IllegalStateException is raised in Java. This exception is utilized to show that a method...

3 minutes read.

Java Enum

Enumerations are used in programming languages to represent collections of named constants. For instance, the four suits in a deck of playing cards might represent four iterators named Club, Diamond,...

3 minutes read.

Longest Arithmetic Progression Sequence in Java

The task is to find the length of the largest sequence in an array to form an arithmetic progression. The array arr[] is given. Longest Arithmetic Progression Sequence in Java Algorithm set...

5 minutes read.

Java 8 filters list

A stream with the components of this stream that match the given predicate is provided by the streaming filter (Predicate predicate). This process is step-by-step. Because these operations are always...

4 minutes read.