Java Enum constructor example

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

No comments:

Post a Comment

Model Context Protocol (MCP) — Complete Guide for Backend Engineers

  Model Context Protocol (MCP) — Complete Guide for Backend Engineers Build Tools, Resources, and AI-Driven Services Using LangChain Moder...

Featured Posts