Searching...
Saturday, 13 December 2014

Java Enum constructor example

21:23

Example

package com.vinod.test;

public class EnumConstructor {

    public static void main(String[] args) {
        // Getting year values
        System.out.println("Year " + Days.YEAR.getValue() + "   days");

        // Getting all values
        for (Days day : Days.values()) {
            System.out.println(day.getValue());

        }
    }

}
enum Days {
    YEAR(365), MONTH(30), WEEK(7);
    private int value;
    private Days(int value) {
        this.value = value;
    }
    public int getValue() {
        return value;
    }
}

Output

Year 365   days
365
30
7

0 comments: