Java-Enumeration Methods

Enumeration Methods

All enumerations automatically contain two predefined methods values( ) and valueOf( ). The values( ) method returns an array that contains a list of the enumeration constants. The valueOf( ) method returns the enumeration content whose value corresponding to the string passed to it. The following program demonstrates the values( ) and valueOf( ) methods. The program uses a for-each style for loop to cycle through the array of constants obtained by calling values( ).



Program Source

enum Fruits {
    Apple, Banana, Cherry, Date, Elderberry

public class Javaapp {

    public static void main(String[] args) {
        Fruits[] allfru = Fruits.values();
        for(Fruits fru : allfru)
        Fruits fru = Fruits.valueOf("Cherry");
        System.out.println("fru = "+fru);

Leave a Comment