Bird
Raised Fist0
JavaHow-ToBeginner · 3 min read

How to Add Method to Enum in Java: Simple Guide

In Java, you can add methods to an enum by defining the method inside the enum body just like in a class. Each enum constant can then use or override this method if needed.
📐

Syntax

An enum in Java can have methods defined inside its body. You write the method like in a normal class, after listing the enum constants.

  • enum EnumName { CONSTANTS; method() { ... } }
  • Methods can be public, private, or abstract.
  • You can override methods for each constant by adding a body after the constant.
java
public enum Day {
    MONDAY, TUESDAY, WEDNESDAY;

    public String greet() {
        return "Hello, it's " + this.name();
    }
}
💻

Example

This example shows an enum Day with a method isWeekend() that returns true for weekend days and false otherwise. It also demonstrates overriding the method for specific constants.

java
public class EnumMethodExample {
    public enum Day {
        MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY,
        SATURDAY {
            @Override
            public boolean isWeekend() {
                return true;
            }
        },
        SUNDAY {
            @Override
            public boolean isWeekend() {
                return true;
            }
        };

        public boolean isWeekend() {
            return false;
        }
    }

    public static void main(String[] args) {
        for (Day day : Day.values()) {
            System.out.println(day + " is weekend? " + day.isWeekend());
        }
    }
}
Output
MONDAY is weekend? false TUESDAY is weekend? false WEDNESDAY is weekend? false THURSDAY is weekend? false FRIDAY is weekend? false SATURDAY is weekend? true SUNDAY is weekend? true
⚠️

Common Pitfalls

Common mistakes when adding methods to enums include:

  • Forgetting the semicolon ; after the enum constants when methods follow.
  • Trying to add abstract methods without overriding them in all constants.
  • Not overriding methods in constants when required, causing compilation errors.

Always place a semicolon after the last enum constant if you add methods or fields.

java
public enum Color {
    RED, GREEN, BLUE // Missing semicolon here

    public String getHex() {
        return "#000000";
    }
}

// Corrected version:
public enum Color {
    RED, GREEN, BLUE;

    public String getHex() {
        return "#000000";
    }
}
📊

Quick Reference

ConceptDescription
Enum constantsList at the top, separated by commas, end with semicolon if methods follow
Method inside enumDefine like a normal class method after constants
Override per constantAdd a body after constant and override methods
Abstract methodsDeclare abstract method and override in all constants
Access enum nameUse this.name() to get constant name
✅

Key Takeaways

Add methods inside enum body after constants with a semicolon separator.
Override methods per constant by adding a body after the constant name.
Always end enum constants with a semicolon if methods or fields follow.
Abstract methods in enums must be overridden by all constants.
Use this.name() to get the enum constant's name inside methods.