Java Enum Methods: How to Use and Override
Learn how to define methods in Java enums, use inherited methods, override behavior per constant, and use enum methods in switch statements.

Java enums are more than named constants. Each enum type is a class, and its constants are instances of that enum type. That means you can define methods on the enum, override them per constant, and use them in control flow. Understanding how Java enum methods work is essential for writing maintainable code that uses enums for state machines, strategy selection, and configuration.
The Methods Every Enum Inherits
Every Java enum implicitly extends java.lang.Enum. That base class provides several methods that are available on every enum type without any extra code.
The most frequently used are values() and valueOf(String). values() returns an array of all constants in declaration order. valueOf(String) returns the constant whose name matches the argument, or throws IllegalArgumentException if no match exists.
public enum Direction {
NORTH, EAST, SOUTH, WEST
}
Direction[] all = Direction.values();
Direction d = Direction.valueOf("EAST");The name() method returns the exact string used in the declaration, and ordinal() returns the zero-based position of the constant. Both are part of Enum and are rarely overridden.
System.out.println(Direction.EAST.name()); // EAST
System.out.println(Direction.EAST.ordinal()); // 1toString() also comes from Enum and returns the same value as name() by default. Many developers override it to provide a more readable representation.
Adding Your Own Methods to an Enum
An enum can declare instance methods just like a regular class. These methods can access the constant's fields and any state you define in the enum.
public enum Planet {
MERCURY(3.303e+23, 2.4397e6),
VENUS(4.869e+24, 6.0518e6),
EARTH(5.976e+24, 6.37814e6);
private final double mass;
private final double radius;
Planet(double mass, double radius) {
this.mass = mass;
this.radius = radius;
}
public double surfaceGravity() {
return 6.67300E-11 * mass / (radius * radius);
}
}Here each constant carries its own mass and radius, and surfaceGravity() uses those fields. This pattern is common when an enum represents a fixed set of options that each need slightly different data or behavior.
Methods on an enum can also be abstract, forcing each constant to provide its own implementation. This is useful when the behavior varies per constant.
public enum Operation {
PLUS { double apply(double x, double y) { return x + y; } },
MINUS { double apply(double x, double y) { return x - y; } },
TIMES { double apply(double x, double y) { return x * y; } },
DIVIDE { double apply(double x, double y) { return x / y; } };
abstract double apply(double x, double y);
}The abstract method must be implemented by every constant. This approach keeps the operation logic attached to the constant that represents it, which is often cleaner than a switch statement in a separate method.
Overriding Methods in Enum Constants
Each enum constant can override a concrete method defined in the enum body. This is different from implementing an abstract method because the default implementation remains available to constants that do not override it.
public enum Status {
ACTIVE {
@Override
public String description() {
return "Currently running";
}
},
INACTIVE,
PENDING;
public String description() {
return "No description available";
}
}ACTIVE overrides description(), while INACTIVE and PENDING use the default. This gives you fine-grained control without forcing every constant to implement the method.
Overriding per constant is useful when most constants share a default behavior but a few need a specialized version. It also keeps the logic close to the constant it belongs to, which improves readability.
Using Enum Methods in Switch Statements
Enums work naturally with switch statements. You can call methods on the enum constant inside each branch.
public String describe(Direction d) {
switch (d) {
case NORTH:
return d.name() + " points up";
case EAST:
return d.name() + " points right";
case SOUTH:
return d.name() + " points down";
case WEST:
return d.name() + " points left";
default:
throw new IllegalArgumentException("Unknown direction: " + d);
}
}The default branch is optional if you have already covered every constant, but it can guard against future additions. A more maintainable alternative is to move the description logic into the enum itself as a method, avoiding the switch entirely.
public enum Direction {
NORTH("up"), EAST("right"), SOUTH("down"), WEST("left");
private final String relativePosition;
Direction(String relativePosition) {
this.relativePosition = relativePosition;
}
public String relativePosition() {
return relativePosition;
}
}Now the switch becomes unnecessary. The method on the enum carries the data, and callers simply invoke direction.relativePosition(). This reduces duplication and keeps related logic together.
Performance and Memory Behavior of Enum Methods
Enum constants are created once when the enum class is loaded, and each constant is a singleton. Calling a method on an enum constant does not allocate new objects per call; any allocation depends on the method body.
The values() method, however, returns a new array on each invocation. If you call values() in a hot loop, you may create unnecessary garbage. Cache the array if you need it frequently.
private static final Direction[] ALL = Direction.values();The valueOf(String) method resolves a constant by name. If you need frequent lookups with a custom key or mapping, a static Map built from the constants is a common and clear approach.
An enum constant can be safely shared across threads as long as it does not expose mutable state. Prefer immutable fields, as in the examples above, and avoid methods that mutate the constant.
Common Mistakes When Writing Enum Methods
One frequent mistake is relying on ordinal() for logic that should be explicit. The ordinal changes if you reorder, add, or remove constants, which can silently break code that depends on it. Use a dedicated field instead.
Another mistake is putting complex business logic inside an enum method that also needs external dependencies. An enum type is not designed to receive injectable dependencies. If a method needs a database connection or a configuration object, it is often better to keep that logic outside the enum and pass the dependency as a parameter.
Finally, be careful with mutable fields in enum constants. Because constants are shared, any mutation affects all callers. Prefer immutable fields and methods that do not change state. If you need stateful behavior, consider whether an enum is the right model or whether a regular class with instances would be safer.
Try matching browser tools
All toolsSmall utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Java guides
All Java guidesJava Final Keyword: Usage and Behavior
How Java's final keyword works for variables, methods, and classes, with practical examples and decision guidance for using it.
Java Final Variable: Syntax, Behavior, and Use Cases
Understand what a final variable in Java means, how initialization works, and when to use it for constants, immutability, and clearer code.
Java Duration vs Period: When to Use Each
java duration vs period: Compare Java Duration and Period to handle time-based and date-based calculations correctly. Learn syntax, conversion, and pitfalls.
Instance vs Static Variables in Java: Key Differences
Understand the difference between instance and static variables in Java, including memory, lifecycle, and when to use each.
More in Java
All Java guidesThe guides filed next to this one in Java, in library order.