Skip to content
Sharp

Core Java · interview question

Which refactoring step can help convert a setter-heavy design to a safer design pattern?

Multiple choiceHardchapter-3-curatedpremiumcore-javacode-reading-and-state-safety
Choose one answer
Answer and explanation

Correct answer

D. Use constructor injection to set all necessary fields

Why

Constructor injection ensures that all necessary fields are initialized at object creation time, reducing the need for setters and increasing safety by maintaining invariants.

Test yourself on Core Java

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

Start free test
All Core Java questions →