Enum method values() in java
A enum static method values() used to retrieve all enum constants as array. It returns array of all enum values that includes constant in the order they're declared.
Syntax
 1 public static com.java.Level[] values()
return : It returns an array of enum type that includes all constants.
enum method values()
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 
 5 enum Level {
 6     LOW, MEDIUM, HIGH;
 7 }
 8 
 9 public class Example {
 10 
 11     public static void main(String[] args) {
 12 
 13         System.out.println(Arrays.toString(Level.values()));
 14     }
 15 }
In the above example, an enum method values() called that returns an array of enum Level constant in the order they are declared, an array of constant are converted to string and print.
Output
 1 [LOW, MEDIUM, HIGH]

Iterate enum constants

Iterate enum constants
 1 package com.java;
 2 
 3 enum Level {
 4     LOW, MEDIUM, HIGH;
 5 }
 6 
 7 public class Example {
 8 
 9     public static void main(String[] args) {
 10 
 11         // iterate each constant
 12         for(Level lv : Level.values()) {
 13             System.out.println(lv);
 14         }
 15     }
 16 }
In the above example, a Level.values() called that returns array of enum type constant. A for loop iterates each elements sequentially in which it is define and print value of each constant.
Output
 1 LOW
 2 MEDIUM
 3 HIGH
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us