Enums with Fields and Methods
Add behavior to enums.
Enums with Fields and Methods is a free Java Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Enums Are Full Classes
A Java enum is far more than a list of constants. It is a real class whose instances are the constants.
That means enums can have fields, constructors, and methods like any other class.
public class Main {
enum Direction { NORTH, SOUTH, EAST, WEST }
public static void main(String[] args) {
Direction d = Direction.NORTH;
System.out.println(d + " is a " + d.getClass().getSuperclass().getSimpleName());
}
}Adding a Field
You can attach data to each constant. Declare a final field and a constructor, then pass values in parentheses after each constant.
public class Main {
enum Planet {
EARTH(9.81), MARS(3.71), JUPITER(24.79);
private final double gravity;
Planet(double gravity) { this.gravity = gravity; }
double gravity() { return gravity; }
}
public static void main(String[] args) {
System.out.println("Mars gravity: " + Planet.MARS.gravity());
}
}The Enum Constructor Is Private
Enum constructors are implicitly private. You cannot call new on an enum; the JVM creates the constants once at class load.
This guarantees a fixed, finite set of instances.
public class Main {
enum Coin {
PENNY(1), NICKEL(5), DIME(10), QUARTER(25);
private final int cents;
Coin(int cents) { this.cents = cents; }
int cents() { return cents; }
}
public static void main(String[] args) {
int total = Coin.QUARTER.cents() + Coin.DIME.cents();
System.out.println("Total cents: " + total);
}
}Multiple Fields
A constant can carry several pieces of data. Here each planet stores both mass and radius.
public class Main {
enum Planet {
EARTH(5.976e24, 6.37814e6),
MARS(6.421e23, 3.3972e6);
private final double mass, radius;
Planet(double mass, double radius) { this.mass = mass; this.radius = radius; }
double surfaceGravity() {
final double G = 6.67300E-11;
return G * mass / (radius * radius);
}
}
public static void main(String[] args) {
System.out.printf("Earth g = %.2f%n", Planet.EARTH.surfaceGravity());
}
}Instance Methods
Enum methods can compute results from the constant's own fields, just like normal instance methods.
public class Main {
enum Size {
SMALL(8), MEDIUM(12), LARGE(16);
private final int ounces;
Size(int ounces) { this.ounces = ounces; }
int ounces() { return ounces; }
double liters() { return ounces * 0.0295735; }
}
public static void main(String[] args) {
System.out.printf("Large = %.2f L%n", Size.LARGE.liters());
}
}Built-in name and ordinal
Every enum gets name() (the exact identifier) and ordinal() (its zero-based position).
Prefer storing your own data over relying on ordinal(), which breaks if you reorder constants.
public class Main {
enum Day { MON, TUE, WED }
public static void main(String[] args) {
for (Day d : Day.values()) {
System.out.println(d.name() + " -> ordinal " + d.ordinal());
}
}
}values and valueOf
The compiler generates two static helpers:
values()returns an array of all constants.valueOf(String)looks up a constant by name, throwing if not found.
public class Main {
enum Status { ACTIVE, PAUSED, CLOSED }
public static void main(String[] args) {
System.out.println("count: " + Status.values().length);
Status s = Status.valueOf("PAUSED");
System.out.println("parsed: " + s);
}
}Overriding toString
You can override toString() to give friendlier labels while keeping the constant name for code.
public class Main {
enum Priority {
LOW, MEDIUM, HIGH;
@Override public String toString() {
return name().charAt(0) + name().substring(1).toLowerCase();
}
}
public static void main(String[] args) {
for (Priority p : Priority.values()) System.out.println(p);
}
}Enums in switch
Enums work cleanly in switch. With modern switch expressions you get exhaustiveness checks and no fall-through.
public class Main {
enum Traffic { RED, YELLOW, GREEN }
static String action(Traffic t) {
return switch (t) {
case RED -> "Stop";
case YELLOW -> "Slow down";
case GREEN -> "Go";
};
}
public static void main(String[] args) {
System.out.println(action(Traffic.GREEN));
}
}Static Lookup Maps
A common pattern is a static map from a field back to the constant, built once in a static initializer.
import java.util.HashMap;
import java.util.Map;
public class Main {
enum Op {
PLUS("+"), MINUS("-");
private final String symbol;
Op(String symbol) { this.symbol = symbol; }
private static final Map<String, Op> BY_SYMBOL = new HashMap<>();
static { for (Op o : values()) BY_SYMBOL.put(o.symbol, o); }
static Op fromSymbol(String s) { return BY_SYMBOL.get(s); }
}
public static void main(String[] args) {
System.out.println(Op.fromSymbol("-"));
}
}Enums Implement Interfaces
An enum can implement an interface, letting constants plug into general-purpose code.
public class Main {
interface Describable { String describe(); }
enum Animal implements Describable {
DOG, CAT;
public String describe() { return "A " + name().toLowerCase(); }
}
public static void main(String[] args) {
Describable d = Animal.DOG;
System.out.println(d.describe());
}
}Quick Check
Test your understanding of enum fields.
Recap
You learned that enums are full classes:
- Constants can hold fields set via a private constructor.
- Enums can have instance methods and override
toString(). values()andvalueOf()are generated for you.- Enums can implement interfaces and prefer fields over
ordinal().
Next, per-constant behavior with abstract methods.
public class Main {
enum Greeting { HELLO; String text() { return "Hi"; } }
public static void main(String[] args) {
System.out.println("Enum fields recap: " + Greeting.HELLO.text());
}
}Frequently asked questions
Is the “Enums with Fields and Methods” lesson free?
Yes — the full text of “Enums with Fields and Methods” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Enums with Fields and Methods”?
Add behavior to enums. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Enums with Fields and Methods” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Enums with Fields and Methods
- Abstract Methods in Enums
- EnumSet
- EnumMap