SupportWriter space ↗
← Back to the journal
Basic

How do I get constants name of an enum?

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 { […]

DWayan · 04 Aug 2009 · 1 min read

Article by Wayan · Source: Kode Java ↗

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);
        }
    }
}

Comments 0

Share your questions, experiences, or feedback about this article.

No comments yet. Be the first to share your feedback.

← Explore more notes