Skip to content
Sharp

Modern Java · interview question

Why do teams use a constructor within a value object to enforce invariants in production code?

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

Correct answer

B. It ensures that all instances are valid upon creation.

Why

By enforcing invariants within a constructor, you ensure that any instance of the value object is valid from the moment it is created. This reduces the risk of invalid states and increases the reliability of the domain model.

Test yourself on Modern Java

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

Start free test
All Modern Java questions →