Skip to content
Related Articles

Related Articles

Improve Article
Java Program to Access All the Constant Defined in the Enum
  • Last Updated : 22 Feb, 2021

An enum is a special class that represents a group of constants. To create an enum, use the enum keyword (instead of class or interface), and separate the constants with a comma.

enum Day{

SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
}

values() method can be used to return all values present inside enum.

We can’t see this method in Javadoc because the compiler adds it. The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all the enum’s values in the order they are declared.

So the values() function list all values of the enumeration.

Day days[] = Day.values();  

for(Day d : days)  
  System.out.print(d);

Java




// Java program to show the usage of  
// values() method of java enumeration  
    
enum Day{ 
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY; 
    
class Main{ 
        
    public static void main(String args[]) 
    
       // Calling values()
       Day days[] = Day.values(); 
        
       for(Day d : days) 
       System.out.println( d ); 
    
}
Output
SUNDAY
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :