Skip to content
Sharp

Core Java · interview question

In an e-commerce application, consider a Customer class that has public fields for id and name. What is a potential risk with this design?

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

Correct answer

B. Other classes can directly change the id and name, risking invalid state.

Why

When fields are public, other classes can modify them directly without any validation checks, potentially leading to invalid or inconsistent states. Encapsulation is violated, and the object's invariants could be compromised.

Test yourself on Core Java

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

Start free test
All Core Java questions →