开发者

restricting values for a string member variable

开发者 https://www.devze.com 2023-01-23 20:24 出处:网络
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 s开发者_开发知识库et to any other value except start

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 s开发者_开发知识库et to any other value except start or data or end ??


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号