Medium👤 3-5 years👤 8-15 years 1 min read

How do you design a correct immutable class for a multi-threaded service?

Asked inAmazonMicrosoftDeloitte
#immutability#thread-safety#defensive copy#records#value object
Report issue

⚡ Short Answer

Make the class final, all fields private final, no setters, initialize in the constructor, and defensively copy any mutable inputs/outputs (collections, dates). Java records do most of this — but you must still deep-copy mutable members.

Coffee Chat Question

Concept Made Simple

How do you design a correct immutable class for a multi-threaded service?

🧠Mind Map Answer

Remember It Faster

final classno subclass can add mutability
private final fieldsset once
no settersno post-construction change
defensive copyin (ctor) and out (getters)

⌨️Hands-on Keyboard

Learn by Doing

java
public final class Money {
    private final long cents;
    private final List<String> tags;
    public Money(long cents, List<String> tags) {
        this.cents = cents;
        this.tags = List.copyOf(tags); // defensive copy in
    }
    public List<String> tags() { return tags; } // already unmodifiable
}

🔥What If?

Think Beyond the Expected

A record holds a List field — is it truly immutable?

No. The record reference is final, but the List itself is still mutable, so callers can change its contents. You must copy it in the compact constructor (List.copyOf) and expose an unmodifiable view.

😂Real World

Immutable value objects (Money, DateRange, Coordinates) are shared freely across threads with zero synchronization — the backbone of thread-safe domain models and functional-style services.

🎯Interviewer's Expectation

Keywords they're listening for:

final class/fieldsno settersdefensive copies of mutable membersrecords caveatthread-safety benefit

⚠️Common Mistakes

  • Storing a mutable collection/date without copying
  • Returning the internal mutable reference from a getter
  • Forgetting final on the class, allowing a mutable subclass

Best Practices

  • Defensive-copy mutable inputs and outputs
  • Prefer records + List.copyOf for value objects
  • Provide with-style copy methods for 'changes'

🔁Follow-up Questions

  • 1How do records help, and where do they fall short?
  • 2How do you 'modify' an immutable object (with-er / copy)?
  • 3Why are immutable objects inherently thread-safe?

🧩Related Technologies

recordsList.copyOfCollections.unmodifiableListLombok @Value

Continue Learning with AI

Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.

Plain-language foundations

I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner.

Topic: OOP (Core Java)
Interview question: "How do you design a correct immutable class for a multi-threaded service?"

Please:
1. Explain the core idea in simple, plain language, using an everyday analogy.
2. Define any technical terms you use.
3. Walk through one small, concrete example.
4. Finish with a single sentence I can easily remember.

Keep the tone friendly and assume I'm new to this topic.
Open inChatGPTGeminiClaude

Was this answer helpful?

Support our platform by exploring our recommended products.

As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.

Related Questions