I means whatever YOU, the designer, has defined it to mean for a particular class.
For instance, let's say you have a bank account:
The "legal state" of a bank account is that the amount of money deposited in the account is greater than or equal to the lower limit. Here is a valid use:
At all times, the account is in a legal state. However, the following code demonstrates that the account can be in an illegal state:
The banked amount is
0, which is less than the lower limit. This should not be possible! The first requirement was violated:
All objects start off in a legal state after creation
We fix this by adding validation to the constructor:
Now, what if a bank account is not constructed with a lower limit, but instead the lower limit is read from a file:
"lower-limit.txt" is supposed to contain the value
-300.
Assume that there is a problem with reading
"lower-limit.txt". Maybe the file doesn't exist or is in the wrong location:
The object transitioned from a legal to an illegal state because it didn't reset before throwing. Here is the fix:
The
bankedAmount is only changed if the
readLowerLimit() call succeeds. If an exception occurs, the account is unchanged, and so remains in a legal state.