Skip to content
Sharp

Modern Java · interview question

Given the following code snippet, identify the primary issue with the 'Email' value object:?

Multiple choiceHardchapter-7-curatedpremiummodern-javavalue-objects
java
class Email {
  private final String address;
  public Email(String address) {
    this.address = address;
  }
  // missing validation
}
Choose one answer
Answer and explanation

Correct answer

B. The Email constructor should be private to enforce validation.

Why

The Email constructor should be private to enforce validation through a static factory method or builder. This ensures that any instance creation goes through necessary validation logic, maintaining the integrity of the value object.

Test yourself on Modern Java

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

Start free test
All Modern Java questions →