Bifalgorithm post · Computer science one · Accessor & Mutator Methods
How can getters and setters validate values before changing private variables?
How can getters and setters validate values before changing private variables? (Computer science one · Accessor & Mutator Methods) — "One advantage to using ge…
Ideas in this flowchart
- Getters and setters provide greater control over variable values than making everything public.
- Making everything public exposes variables without the same level of control.
- Setter methods can control which values are accepted.
- Data validation can reject unacceptable values.
- Null values may be rejected.
- Invalid values may be rejected.
- The setFirstName method validates the first-name value.
- A null first name causes an exception stating that names cannot be null.
- A non-null first name is assigned to the firstName variable.
- The setGpa method treats values below 0.0 or above 4.0 as invalid.