Joe Soltzberg

20%
Flag icon
Instance fields of public classes should rarely be public (Item 16). If an instance field is nonfinal or is a reference to a mutable object, then by making it public, you give up the ability to limit the values that can be stored in the field. This means you give up the ability to enforce invariants involving the field. Also, you give up the ability to take any action when the field is modified, so classes with public mutable fields are not generally thread-safe. Even if a field is final and refers to an immutable object, by making it public you give up the flexibility to switch to a new ...more
Joe Soltzberg
The last point is a good argument against 'no need to abstract until necessary' with respect to getters.
Brian
· Flag
Brian
oh, interesting. you're taking it further and saying not to reveal the underlying data at all until needed. good point. think about what of the representation needs to be exposed, and only provide acc…
Effective Java
Rate this book
Clear rating
Open Preview