Skip to content
Sharp

Object-oriented programming · interview question

How would you explain an effective way to ensure immutability in a 'Customer' class in a Java application in an interview?

Multiple choiceHardchapter-3-curatedpremiumoopaccess-modifiers-and-visibility
Choose one answer
Answer and explanation

Correct answer

C. Use final for fields and avoid setters.

Why

To ensure immutability, declare fields as final to prevent reassignment and avoid providing setters. This ensures that once the object is constructed, its state cannot be altered.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →