Skip to content
Sharp

Object-oriented programming · interview question

Consider the following code snippet. What is one way to ensure that the 'Customer' class is immutable?

Multiple choiceHardchapter-3-curatedpremiumoopimmutability-basics
java
class Customer { private final String name; private final String email; }
Choose one answer
Answer and explanation

Correct answer

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

Why

To make a class immutable, all fields should be private and final, ensuring they cannot be changed after the object is constructed, and no setters should be provided.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →