Quick revision
The short answer
An immutable class is one whose state cannot change after construction. We get there with six rules: make every field private and final, provide no setters or other methods that change state, stop subclassing with a final class or private constructors, copy mutable inputs in the constructor and never hand out internal mutable objects, do not let this escape during construction, and make every change return a new instance, as String and BigDecimal do. Each rule closes a different way the state could change or be seen half-built.
What to remember
In this answer, we saw the rules for an immutable class and why each one exists. Private final fields are assigned once and become safely visible to other threads; no methods change state; a final class or private constructors stop subclasses from adding mutability; copying mutable inputs and never exposing internal objects stops changes through shared references; not letting this escape keeps others from seeing a half-built object; and operations return new instances as String and BigDecimal do. Records give us the first three rules, but we still copy mutable components ourselves.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
An immutable class is one whose state cannot change after construction. We get there with six rules: make every field private and final, provide no setters or other methods that change state, stop subclassing with a final class or private constructors, copy mutable inputs in the constructor and never hand out internal mutable objects, do not let this escape during construction, and make every change return a new instance, as String and BigDecimal do. Each rule closes a different way the state could change or be seen half-built.
Why this question is asked
Immutable objects are easy to reason about: once created, they never change, so we can share them between threads, use them as map keys and cache them without fear. Interviewers ask for the rules because each one blocks a specific way that a supposedly immutable class can still change, and many real bugs come from following four rules and forgetting the fifth. In this answer, we will start with a class that looks immutable but is not, then go through the rules one at a time with the reason for each, and finish by looking at what records give us for free.
1A Quick Recap: What Immutable Means
An object is immutable when its observable state, the values any caller can see through its methods, is fixed when the constructor finishes and never changes afterwards. String is the classic example: "abc".toUpperCase() does not change the original string; it returns a new one.
To see why we need several rules, here is a class that a developer believed was immutable. It has no setters, yet its state can change in three different ways.
A third way is a subclass that overrides bounds() to return something different each time. Each of the rules below closes one of these doors.
2Rule 1: Make All Fields Private and Final
private stops other classes from reaching the field directly. final means the field is assigned exactly once, in the constructor, and the compiler rejects any later assignment. That alone prevents a whole class of accidental changes.
final also has a second, less visible effect. The Java Memory Model, the rules that say when one thread sees another thread's writes, guarantees that any thread that sees a properly constructed object also sees the values its constructor wrote to final fields, even if the object was shared without synchronisation. Without final, another thread could in theory see a default value such as null or 0. So final is part of what makes an immutable object safe to share.
3Rule 2: Provide No Methods That Change State
This is the rule everyone remembers: no setters. But it means more than setX methods. Any method that changes a field, adds to an internal collection or modifies an internal array breaks immutability, whatever its name. A method called normalize() that trims a stored string in place is a setter in disguise.
With final fields, the compiler already prevents reassigning the fields, so this rule mostly matters for fields that refer to mutable objects, which is where the next rules come in.
4Rule 3: Prevent Subclassing
If the class is not final, anyone can extend it and add mutable fields or override methods so they return changing values. Code that receives our type could then be given a subclass instance that behaves differently, which breaks every guarantee the caller relied on. So we declare the class final, or make all constructors private and offer static factory methods, which has the same effect because a subclass must call a superclass constructor.
Private constructors with factories also let us return cached instances later, as Integer.valueOf does, without changing the API.
5Rule 4: Copy Mutable Inputs and Never Expose Internal Objects
A final field that refers to a mutable object, such as an ArrayList, a Date or an array, only stops reassignment of the reference. The object itself can still change. So we copy mutable inputs in the constructor, making a defensive copy that only our object can reach, and we never return the internal object itself. Here is the Range class with all the rules applied.
| Rule | What goes wrong without it |
|---|---|
| private final fields | fields reassigned; other threads may see default values |
| no state-changing methods | callers change the object after creation |
| final class or private constructors | a subclass adds mutable state or overrides methods |
| copy inputs, do not expose internals | changes through the caller's or getter's reference |
| do not let this escape | other code sees the object before it is fully built |
| changes return new instances | no way to compute new values without mutation |
Notice that because the copy is unmodifiable, the getter can return it directly with no further copying. If we stored a mutable copy, for example a new ArrayList, the getter would have to copy again on every call. For arrays, which can never be made unmodifiable, we must clone both on the way in and on the way out.
6Rule 5: Do Not Let this Escape During Construction
The final field guarantee only holds after the constructor finishes. If the constructor passes this to other code, for example by registering itself as a listener or starting a thread that uses it, that code can see the object while fields are still unset.
The fix is to finish construction first and register afterwards, typically in a static factory method: create the object, then call bus.register(watcher), then return it.
7Rule 6: Return New Instances for Changes
Immutable objects still need operations that produce different values. Instead of changing the object, such methods return a new one, and the original stays as it was. That is how String, BigDecimal and the java.time types work.
The last two lines show the trap on the caller's side: forgetting to use the returned value. Many IDEs and static analysis tools warn about ignored results of such methods.
8Follow-up questions
Is a record automatically immutable?
Only shallowly. A record's fields are private and final, it has no setters, and records are implicitly final, so rules 1 to 3 come for free. But a record stores whatever references we pass to it: record Team(List<String> members) keeps the caller's list, and its accessor returns that same list. To make it truly immutable, we copy mutable components in a compact constructor, for example members = List.copyOf(members).
Do all fields have to be final?
Strictly, the observable state must not change. String has a non-final hash field that caches its hashCode on first use; it is computed from immutable data and always gives the same result, so the class is still effectively immutable. That trick needs care, and for our own classes making every field final is the safe default.
Why are immutable objects good map keys?
Because their hashCode cannot change after insertion. A mutable key that changes after being put into a HashMap ends up in the wrong bucket, and get() can no longer find it. An immutable key keeps its hash for its whole life.
9Conclusion
In this answer, we saw the rules for an immutable class and why each one exists. Private final fields are assigned once and become safely visible to other threads; no methods change state; a final class or private constructors stop subclasses from adding mutability; copying mutable inputs and never exposing internal objects stops changes through shared references; not letting this escape keeps others from seeing a half-built object; and operations return new instances as String and BigDecimal do. Records give us the first three rules, but we still copy mutable components ourselves.
Open with
An immutable class has state that is fixed once the constructor returns, and there are six rules I follow. All fields are private and final, which stops reassignment and also gives the memory-model guarantee that other threads see the constructor's values without extra synchronisation.
Then, if they want more
There are no setters or any other methods that change state. The class is final, or has only private constructors with factories, so a subclass cannot add mutable fields or override methods.
Mutable inputs such as lists, arrays or Date objects are copied in the constructor, and getters never hand out internal mutable objects; storing List.copyOf of the input is neat because the copy is unmodifiable, so the getter can return it directly.
The constructor must not let this escape, for example by registering a listener, because the final field guarantee only holds after construction completes. And operations that would change the value return a new instance, like String.toUpperCase or BigDecimal.add.
If asked about records, I would say they give the first three rules for free, but they are only shallowly immutable, so I still copy mutable components in a compact constructor.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "List the rules for writing an immutable class in Java, and explain why each rule matters.": https://scrivio-interview-library.netlify.app/java/immutability/b6620cfc14/
Java fundamentals, question 4 of 16 Back to the path
More in Immutability and defensive copying