×

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

Java program to count the occurrences of each character

The count of each character is the frequency of the characters. For example, the given String is “Programming”. In the given string, the count of the characters ‘P’ -1, ’r’-2,...

6 minutes read.

Different Ways to Take Input from User in Java

Any information provided to a system for use is known as user input. Any responsive software or application must include user input. In Java, there are 4 different ways to...

5 minutes read.

Internal Working of ArrayList in Java

Java's version of a resizable array is called ArrayList. The dynamic growth of an array list ensures that there is always room for new elements. ArrayList's backing data structure is...

8 minutes read.

Differences between Lock and Monitor in Java Concurrency

In this tutorial, we will discuss the overview of Lock and Monitor and the differences between them. Introduction Java Concurrency is the ability to perform specific tasks at a time parallelly. The...

4 minutes read.

How to download Eclipse for Java

Introduction: We write a java program, and when we want to run it, we need software to run it. Eclipse is a kind of software used to execute a JavaFX...

3 minutes read.

Advanced Java Viva Questions

One of the more difficult languages available now is Java. Currently, 10 thousand developers worldwide use the programming language, which is rising daily. So, if you're a Java developer, an aspiring...

9 minutes read.

String Programs in Java

String Programs in Java: In Java, a String is an immutable object that represents a sequence of characters. For example, “Tutorial” is a string that consists of 8 characters: ‘T’,...

10 minutes read.

Java Code Optimization

We encounter the idea of optimization while working on any Java application. It is essential that the code we write is not only clear and error-free but also optimized, meaning...

9 minutes read.

Java Enum Keyword

Definition: A data type in Java called Enum has a respect to supply of constants. The weekdays (SUN, MON, TUE, WED, THU, FRI, and SAT), directions (NORTH, SOUTH, EAST, and WEST),...

4 minutes read.

Reserved Keywords in Java

In Java, a reserved term is used as a code key called a keyword. Because they are predefined, these terms cannot be used for anything else. They cannot serve as...

3 minutes read.

Switch Case with Enum in Java

From some conditions, the java switch statement executes one statement. Similar to the If-Else-If ladder statement, this can be Byte, short, int, long, enum, string, and some wrapper types like...

4 minutes read.

Binary Strings Without Consecutive Ones in Java

N, an integer, is provided. Our objective is to determine the overall number of strings with length N that do not include successive 1s. Example: Input: 3 Output: 6 Explanation The binary forms of length...

6 minutes read.

Misc Operators in Java

In this article, we are going to learn about the misc operators in Java. Misc operators are nothing but the miscellaneous operators. The java programming language supports some of the...

4 minutes read.

Java Enum vs Class

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 integrators named Club, Diamond,...

7 minutes read.

How to encrypt password in Java

Every software program needs a username and password to identify a legitimate user. A username can be any number of things, including an email address or a string of characters....

6 minutes read.

User Defined Exception in Java

An exception is an error (run time error) that happened while a program was being executed. The program stops abruptly whenever the Exception occurs, and the code after the line...

3 minutes read.

How to create an array in Java

An array is a linear data structure stores a collection of fixed number ofelements of similar type. The size of an array is specified when it is created. The array...

14 minutes read.

Java Map Interface

A map is a collection that maps keys to values, with no duplicate keys allowed. The elements in a map are key/value pairs. HashMap: HashMap stores the keys in a...

3 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.

Balanced Prime Number in Java

This section will cover the definition of a balanced prime number as well as how to find one using a Java program. Balance Prime Number A prime number that is equivalent to...

5 minutes read.