Skip to content
Sharp

Modern Java · interview question

Why should validation logic be implemented inside a value object's constructor?

Multiple choiceMediumchapter-7-curatedpremiummodern-javavalue-objects
Choose one answer
Answer and explanation

Correct answer

A. To ensure object creation with valid state

Why

By implementing validation logic in the constructor, you ensure that a value object is always created in a valid state. This helps maintain invariants and prevents invalid data from propagating through the system.

Test yourself on Modern Java

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

Start free test
All Modern Java questions →