Sub topic 12
Record, Enum, or Class?
You now know three ways to make your own type in Java: enums, records, and regular classes. Each one fits a different job. This chapter gives you a simple way to choose.
Three Questions to Ask
A type is a kind of value your program works with, such as Day or Point. When you need a new type, ask these questions in order.
- Is there a fixed list of possible values that you know in advance? Use an enum. An enum is a type with a set of named constants, and no others can ever be created. Days of the week, coin sizes, and traffic light colors all fit.
- Is it just data that should not change after you create it? Use a record. A record is a short way to declare a class that holds data. Java writes the constructor, accessors,
equals,hashCode, andtoStringfor you. - Does it need to change over time, hide its data, or extend another class? Use a regular class. A regular class gives you full control, but you write more code.
Comparing the Three
| Feature | Enum | Record | Regular class |
|---|---|---|---|
| Number of instances | Fixed, listed in the code | As many as you create | As many as you create |
| Fields can change | No, if you make them `final` | No, all fields are `final` | Yes, if you allow it |
| Generated `equals` and `toString` | Yes, built in | Yes, based on the data | No, you write them |
| Can extend another class | No | No | Yes |
| Can implement interfaces | Yes | Yes | Yes |
| Best for | Choices and categories | Values and messages | Objects with changing state |
A Worked Example
Picture a small coffee shop app. It has three ideas in it:
- A drink size can only be small, medium, or large. That is a fixed list, so it is an enum.
- An order is an item name plus a size. Once placed, it never changes. That is plain data, so it is a record.
- A cart holds orders, and you add more to it over time. Its contents change, so it is a regular class.
import java.util.ArrayList;
import java.util.List;
enum Size { SMALL, MEDIUM, LARGE }
record Order(String item, Size size) {}
class Cart {
private final List<Order> orders = new ArrayList<>();
void add(Order order) {
orders.add(order);
}
int count() {
return orders.size();
}
}
public class Main {
public static void main(String[] args) {
Cart cart = new Cart();
cart.add(new Order("Coffee", Size.LARGE));
cart.add(new Order("Tea", Size.SMALL));
System.out.println(cart.count()); // 2
System.out.println(new Order("Tea", Size.SMALL)); // Order[item=Tea, size=SMALL]
}
}Save this as Main.java, then run java Main.java with JDK 17 or later.
Each type is doing only its own job. Cart keeps its list private, so other code can only change it through add. A record could not do that, because a record's data is meant to be read by anyone through its accessors.
Common Mistakes
- Using strings or numbers instead of an enum. Writing
String size = "lareg";compiles, but the typo becomes a bug.Size.LAREGdoes not compile, so the compiler catches the mistake for you. - Using a record for something that changes. A bank account balance goes up and down. A record cannot update its fields, so a class fits better.
- Using an enum for data loaded at runtime. If users can add new categories while the app runs, an enum cannot hold them. Enum constants are fixed when you compile the code.
Exercise
You are modeling a chess game. Pick enum, record, or class for each of these: (a) the piece types: king, queen, rook, bishop, knight, pawn; (b) a board square with a column and a row; (c) the game, which tracks whose turn it is and where every piece stands.
Show answer
(a) Enum, because the set of piece types is fixed and known. (b) Record, because a square is plain data that never changes, such as Square(char column, int row). (c) Regular class, because the game's state changes with every move.
Exercise
A teammate writes record Counter(int value) {} and wants to add a method increment() that makes value one bigger. Why will this not work, and what should they use instead?
Show answer
Record fields are final, so a method cannot change value after the record is created. They can use a regular class with a private, non-final field, or have increment() return a new record: Counter increment() { return new Counter(value + 1); }.