Skip to content
Sharp

Engineering Interviews · interview question

How can you ensure a class is immutable in Java?

Multiple choiceMediumchapter-20-curatedpremiumengineering-interviewscode-review-spotting-bugs
Choose one answer
Answer and explanation

Correct answer

A. Make all fields final and private, and provide no setters

Why

To make a class immutable, all fields should be declared final and private with no setters provided. This ensures the state of an object cannot change once it is constructed.

Test yourself on Engineering Interviews

A free test with instant, explained results. No account needed.

Start free test
All Engineering Interviews questions →