- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Access All the Constant Defined in the Enum
After JDK version 5 java introduces the enumeration. It is a group of constants defined using the keyword ‘enum’. The final variables in java are somewhat similar to enumeration.
In this article, we will make a java program in which we define an enum class and try to access all the constant defined in the enum by using valueOf() and values() methods.
Enum
We use enum class when we need to define a fixed set of constants. For example, if we want to use the days of the week, planet names, names of all five vowels etc. Note that all the names of constants are declared in uppercase letters.
Although enum is a class type in java, we can’t instantiate it. Each constant defined in an enum is the instance of its enum type. It provides other functionalities of a class like we can create its instance variables, methods and constructors.
Syntax
Enum enumeration_name { // constants values }
Example
enum Planet { EARTH, MARS, JUPYTER, NEPTUNE, SATURN, URANUS, VENUS; }
The above example illustrates how we can create an enum class. Name of the enum is Planet and the variables EARTH, MARS are its constants. By default, they are declared as public and static. These constants are self-typed because their type is defined by the enumeration in which they are defined. In this case, they are of Planet type.
Program to Access all Constant defined in the Enum
valueOf() and values() are the two predefined methods that will help us to get the values of constants defined in the list.
valueOf() − It returns the value of constant that is passed as argument.
values() − It returns an array that contains all the constants present in enum class.
Example 1
The following example demonstrates how we can get the constants by using valueOf() method &minuns;
enum Vibgyor { // creating enumeration VIOLET, INDIGO, BLUE, GREEN, YELLOW, ORANGE, RED; // constants } public class Main { public static void main(String[] args) { System.out.println("The color present in VIBGYOR: "); Vibgyor color = Vibgyor.ORANGE; System.out.println(color + " is present at index: " + color.ordinal()); Vibgyor colr = Vibgyor.valueOf("INDIGO"); System.out.println(colr); System.out.print(Vibgyor.valueOf("RED")); } }
Output
The color present in VIBGYOR: ORANGE is present at index: 5 INDIGO RED
In the above program, we have created an enum ‘Vibgyor’ with some constants. The two variables ‘color’ and ‘colr’ are the enumeration variable of type Vibgyor. Using these variables we have fetched the constants. Using valueOf() method we can get one item at a time. The ordinal() method is used here to access the index value of ORANGE. Indexing starts from 0. We can also access the constant without creating any variable.
Example 2
The following example demonstrates how we can get the constants by using values() method −
enum Vibgyor { VIOLET, INDIGO, BLUE, GREEN, YELLOW, ORANGE, RED; } public class Main { public static void main(String[] args) { System.out.println("The color present in VIBGYOR: "); // for each loop to iterate through all constants for(Vibgyor color : Vibgyor.values()) { System.out.println(color); } } }
Output
The color present in VIBGYOR: VIOLET INDIGO BLUE GREEN YELLOW ORANGE RED
In the above program, we have accessed all the constants with a single for each loop. values() method can access multiple values at a time.
Conclusion
In this article, we have discussed enum and its methods. We have created java program to understand how we can access all the constants defined in the enum using values() and valueOf() methods.