What is a Value Object?
Learn what a value object is, why it is immutable, and how it differs from an entity, with a Java Money example.
Expected Interview Answer
A value object is an object defined entirely by its attribute values rather than a unique identity, meaning two value objects with the same data are interchangeable and equal, and value objects are typically made immutable.
Unlike an entity, which is tracked by a stable identifier across its lifetime even as its attributes change, a value object has no independent identity at all — an instance representing “$50” is fully interchangeable with any other instance representing “$50.” Because there is no identity to protect, value objects are usually designed as immutable: any “change” produces a new value object rather than mutating the existing one, which eliminates a whole class of aliasing and shared-mutable-state bugs. Classic examples include Money, DateRange, Coordinate and Color. This concept comes from domain-driven design, where correctly distinguishing entities from value objects is one of the first and most important modeling decisions in a domain.
- Immutability eliminates aliasing and shared-state bugs
- Equality is simple and predictable (compare all attributes)
- Safe to freely share and cache instances
- Clarifies domain modeling by separating identity concerns from data concerns
AI Mentor Explanation
A “boundary” worth four runs is just four runs, wherever and whenever it’s scored — one four is fully interchangeable with any other four, there’s no individual identity attached to a specific boundary. Two “Four” values are simply equal because they represent the same quantity. A value object works this same way: it’s defined purely by its data, so two instances with the same value are interchangeable, unlike a specific player who has a persistent identity across the whole match.
Step-by-Step Explanation
Step 1
Identify data with no independent identity
Ask: does this concept need tracking across time, or is it fully described by its data (e.g. Money)?
Step 2
Model it as immutable
Make all fields final/read-only so instances cannot be mutated after creation.
Step 3
Implement value-based equality
Override equals()/hashCode() to compare all attributes, not object identity.
Step 4
"Changes" produce new instances
Any modification returns a brand-new value object rather than mutating the existing one.
What Interviewer Expects
- Clear contrast between value objects and entities
- Mention of immutability as the default design
- A concrete example (Money, DateRange, Coordinate)
- Awareness this comes from domain-driven design
Common Mistakes
- Confusing value objects with entities that happen to be simple
- Making a value object mutable, reintroducing aliasing bugs
- Forgetting to override equals/hashCode, leaving default reference equality
- Assuming all data classes are automatically value objects (they must also lack identity semantically)
Best Answer (HR Friendly)
“A value object is an object that’s defined completely by its data rather than having its own identity, so two value objects holding the same information are considered equal and interchangeable. Things like an amount of money or a date range are typically modeled as value objects, and they’re usually made immutable to avoid bugs.”
Code Example
public final class Money {
private final long cents;
private final String currency;
public Money(long cents, String currency) {
this.cents = cents;
this.currency = currency;
}
public Money add(Money other) {
if (!currency.equals(other.currency)) throw new IllegalArgumentException();
return new Money(this.cents + other.cents, currency); // new instance, no mutation
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Money m)) return false;
return cents == m.cents && currency.equals(m.currency);
}
@Override
public int hashCode() { return Objects.hash(cents, currency); }
}Follow-up Questions
- How does a value object differ from an entity?
- Why are value objects usually made immutable?
- What happens when a value object needs to “change” state?
- Can you give an example of a value object outside of Money?
MCQ Practice
1. A value object is primarily defined by?
Value objects have no independent identity — they are fully described by their data.
2. Two value objects with identical attributes are typically considered?
Value-based equality means matching attributes make instances equal and interchangeable.
3. The recommended default design for a value object is?
Immutability avoids aliasing bugs since there is no identity to protect via controlled mutation.
Flash Cards
Value object in one line? — An object defined entirely by its attribute values, with no independent identity.
Typical mutability? — Immutable — changes produce new instances.
Classic example? — Money, DateRange, Coordinate, Color.
Origin of the concept? — Domain-driven design (DDD), contrasted with entities.