Enum methods and properties

Java provides built-in methods for enums, and each of these methods serves a specific purpose:

Example Program: Enum in Temple Darshan System

Here’s a complete program demonstrating enum usage for different types of Darshan in a temple:

public class TempleDarshan {

    public enum DarshanType {
        GENERAL("Free", 30),
        SPECIAL("Paid", 15),
        VIP("Paid", 5);

        private String type;
        private int waitTime;

        DarshanType(String type, int waitTime) {
            this.type = type;
            this.waitTime = waitTime;
        }

        public String getType() {
            return type;
        }

        public int getWaitTime() {
            return waitTime;
        }
    }

    public static void main(String[] args) {
        DarshanType selectedDarshan = DarshanType.SPECIAL;

        System.out.println("Darshan Type: " + selectedDarshan);
        System.out.println("Category: " + selectedDarshan.getType());
        System.out.println("Estimated Wait Time: " + selectedDarshan.getWaitTime() + " mins");

        System.out.println("All Darshan Types:");
        for (DarshanType type : DarshanType.values()) {
            System.out.println(type + " - " + type.getType() + " - " + type.getWaitTime() + " mins");
        }
    }
}
/*
Darshan Type: SPECIAL
Category: Paid
Estimated Wait Time: 15 mins
All Darshan Types:
GENERAL - Free - 30 mins
SPECIAL - Paid - 15 mins
VIP - Paid - 5 mins
*/

Enums in Java are a powerful mechanism to define a set of named constants that are type-safe and easy to maintain. They provide built-in methods like values(), ordinal(), name(), and valueOf() for efficient handling of constants. Moreover, enums can be extended with constructors, methods, and abstract methods, allowing them to function similarly to classes. This capability makes enums highly versatile, suitable for representing fixed sets of constants such as account types in banking systems, darshan types in temples, and more. By leveraging enums effectively, developers can create cleaner, more maintainable, and readable code structures.

Scroll to Top