-------------------------------------- Java Sag is under construction: How do I get constants name of an enum?
Please inform us if you find a non good advertisement : info.enim dot gmail.com

Best Web Hosting

mercredi 9 mai 2012

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

Aucun commentaire:

Enregistrer un commentaire

Best Web Hosting