How do I get constants name of an enum?

Category: fundamental, viewed: 741 time(s).

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.example.fundametal;

enum Month {
    JANUARY,
    FEBRUARY,
    MARCH,
    APRIL,
    MAY,
    JUNE,
    JULY,
    AUGUST,
    SEPTEMBER,
    OCTOBER,
    NOVEMBER,
    DECEMBER
}

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 user for each statement to print each enumeration
        // constant.
        //
        for (Month month : Month.values()) {
            System.out.println("Month: " + month);
        }
    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Can't find what you are looking for? Join our FORUMS and ask some questions!
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats