To get the constants name of an enumeration you can use the values()
method of the enumeration type. This method return an array that contains a list of enumeration constants.
package org.kodejava.basic;
public enum Month {
JANUARY,
FEBRUARY,
MARCH,
APRIL,
MAY,
JUNE,
JULY,
AUGUST,
SEPTEMBER,
OCTOBER,
NOVEMBER,
DECEMBER
}
package org.kodejava.basic;
public class EnumValuesTest {
public static void main(String[] args) {
// values() method return an array that contains a list of the
// enumeration constants.
Month[] months = Month.values();
System.out.println("Month size: " + months.length);
// We can use for each statement to print each enumeration
// constant.
for (Month month : Month.values()) {
System.out.println("Month: " + month);
}
}
}
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024