By ensuring all changes are made through controlled methods.
Encapsulation helps maintain class invariants by restricting how fields are accessed and modified. This means that any change to a field will be mediated by methods that enforce the class's rules. In a `UserAccount` class, encapsulating the `balance` field with deposit and withdraw methods ensures the balance never falls below zero.
Additional Notes
How does encapsulation help maintain invariants in a class?