×

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

The Diamond Operator in Java 7

The Diamond operator is a comparatively recent Java operator originally developed in JDK 7 to enhance type identification and eliminate boilerplate Java code. The Diamond operator is characterized by a...

2 minutes read.

Enum Java Interview Questions

1. What exactly is Java? Java is a portable, high-level, object-oriented, robust, secure, platform-independent, multi-threaded, high-performance programming language. Developed in June 1991 by James Gosling, also known as Platform. 2. Enum is...

3 minutes read.

Moran Numbers in Java

In this article, we will be acknowledged about the moran numbers in Java, how they are formed, what are the approaches to achieve the moran numbers. Moran Number A moran numbers are...

3 minutes read.

Beautiful Array in Java

In this section, we will be acknowledged about the beautiful array in Java, its characteristics, how it is achieved. What are the types of approaches and what are the tools...

8 minutes read.

How to run java program in ubuntu

To run the java programming in ubuntu, we need to follow several steps: Let's see the process. Step1: Install the Java compiler or JDK to the system. To run the java program, we...

3 minutes read.

Java Long Keyword

Long is a primitive data type in Java. To initilize variables, we implement the long keyword. It can also be applied to techniques. A 64-bit two's complement integer can put...

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

Java TreeMap

TreeMap in Java with Example Java TreeMap implements the NavigableMap interface. It extends Map Interface. Java TreeMap is based on the red-black Tree implementation. It stores the key-value pair in sorted...

7 minutes read.

Zebra Puzzle Problem in Java

Complex puzzles like the zebra puzzle demand a lot of work and mental training to complete. Because it was created by renowned German scientist Albert Einstein, it is also sometimes...

10 minutes read.

Tilt operator in Java | Tilde operator in Java Example

The symbol designates it as a unary operator (pronounced as the tilde). It gives back the bit's complement or inverse. Every 0 turns into a 1, and every 1 back...

3 minutes read.

Java String toLowerCase() methods

Java String toLowerCase() method is used to convert all the characters of the String into lower case. Syntax: public String toLowerCase()              public String toLowerCase(Locale locale) Returns: It returns Lower...

1 minute read.

Java Boolean logicalAnd() Method

The logicalAnd() method of Java Boolean class returns the result of implementing logicalAND operation on the specified Boolean operands. Syntax:public static boolean logicalAnd (boolean a, boolean b) Parameters:The parameters ‘a’ and ‘b’...

2 minutes read.

Java Thread Dump Analyzer

Thread: A thread is a PC program that is stacked into the PC's memory and is under execution. It tends to be executed by a processor or a bunch of processors....

15 minutes read.

Java String endsWith() method

Checks whether this String ends with the specified suffix or not. Syntax public boolean endsWith(String Suffix) Parameter Suffix : It takes suffix as a parameter  i.e. Sequence of characters Returns It returns true when the current...

1 minute read.

3N+1 problem program in Java

The 3N+1 problem is a hypothesis in the field of abstract mathematics (not yet proven). Collectively known as the Collatz problem. This tutorial will describe about the 3N+1 problem and...

3 minutes read.

Streams in Java

The conventional Java SE 8 version came with many new peculiarities, out of which the most striking of which are assuredly lambda expressions and the method references. Streams and Streams...

14 minutes read.

Difference between C, C++, java

C Language: C language is a procedure oriented language. It has been invented by Dennis Ritchie in the year 1970. It is one of the computer programming language. The purpose of...

3 minutes read.

Java Public Keyword

A Java access modifier is a public keyword. It can be applied to classes, constructors, methods, and variables. It is the type of access modifier that is least constrained. A...

4 minutes read.

Java Finally Keyword

The final block in Java is used to run essential code, such as connection closure, among other things. Whether an exception is resolved or not, the Java finally block has...

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.