How to define enum inside as a class?

Java




import java.io.*;
  
class GFG {
      enum Color{
        RED, GREEN, BLUE
    }
    public static void main (String[] args) 
    {
      Color myCol = Color.GREEN;
        System.out.println(myCol);
    }
}


Output

GREEN


To know more about Enum, refer to Enum in Java article.

Converting a String to an Enum in Java

In Java, an enumeration, commonly known as Enum, is a special data type that allows us to define a fixed set of named values or constants. Enums provide a way to represent a predefined list of elements with distinct identifiers and it makes our code more readable and maintainable.

In this article, we will learn how to convert a string into an enum type.

Basic Understanding of Java Enum

Before diving into the String to Enum conversion, a basic understanding of Java enum is required. Here is an example of simple enum code to define enum types and declare constants within enums.

public enum Color{
      RED, GREEN, BLUE
}

We can access enum constants with a dot syntax.

Color myCol = Color.GREEN;

Similar Reads

How to define enum inside as a class?

Java import java.io.*;    class GFG {       enum Color{         RED, GREEN, BLUE     }     public static void main (String[] args)      {       Color myCol = Color.GREEN;         System.out.println(myCol);     } }...

Method to convert Strings to Enums in Java

...