Pan
Pan

Reputation: 6645

restricting values for a string member variable

A string variable in Java can take one of the three values type=start, type=data, type=end .. what would be the best way to make sure it never gets set to any other value except start or data or end ??

Upvotes: 0

Views: 811

Answers (1)

Dave Costa
Dave Costa

Reputation: 48121

Consider using an Enum instead of a String.

Failing that, make it a private member and use a setter method that validates the value. Of course, this won't protect you against code within the class itself directly setting the value.

Upvotes: 3

Related Questions