Java: Code Reading and State Safety
Why is using setters cautiously important?
Too many setters can lead to fragile code and broken invariants. Excessive use of setters can make it…
View Card →Quick study sessions to strengthen memory and retain key concepts.
Why is using setters cautiously important?
Too many setters can lead to fragile code and broken invariants. Excessive use of setters can make it…
View Card →How would you explain a safer approach to handle mutable collections in a class in an interview?
Return a copy or unmodifiable view of the collection. To prevent modifications from outside the class, return a…
View Card →What is immutability in Java?
An immutable object cannot change its state after creation. Immutability means once an object is created, its state…
View Card →How can exposing mutable static fields be problematic?
It can lead to shared state modifications across instances and threads. Mutable static fields are shared across all…
View Card →What are compile-safe but runtime-unsafe designs?
Designs that compile but allow invalid states or operations at runtime. A compile-safe design passes syntax checks but…
View Card →How can a developer refactor a class to improve encapsulation?
Use private fields with public getters and setters. Refactoring a class to use private fields with public getters…
View Card →Why is it problematic to mix public fields and methods in a class?
Mixing public fields and methods breaks encapsulation. When a class uses both public fields and public methods, it…
View Card →What does encapsulation achieve in object-oriented design?
Encapsulation hides internal details and exposes only necessary parts. Encapsulation allows an object to manage its own state…
View Card →How would you explain a common mistake when exposing collections in a class in an interview?
Exposing collections can lead to unintended modifications. If a class exposes a collection directly, external code can modify…
View Card →How can immutability enhance thread safety?
Immutable objects can't change state, preventing race conditions. Immutability ensures that once an object is created, its state…
View Card →