• Post Reply Bookmark Topic Watch Topic
  • New Topic

Restricting the field of a class to certain values  RSS feed

 
N Fowl
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the best way to restrict the values of a field to certain values? In this case it is a string field, and the values to restrict to are a set of 4 different strings.

It has to be done in the class itself. I'm wondering how to do this. I know it might be that you create an if statement in the constructor and check for the values, but I'm not sure how you then tell the constructor to cease constructing or delete the object it has constructed.

Or maybe there is another way entirely to restrict the values.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a private validation method that your constructor calls (any setter for that variable would also call this method). If there are only a few valid values, you can use a switch statemnent to check:


If there are a lot of values, put them in an ArrayList and check by using mylist.contains(value).

Note: Do not call the setter method from the constructor - constructors should not call any public method.
 
N Fowl
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I will probably just use this code directly in the constructor.
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:Note: Do not call the setter method from the constructor - constructors should not call any public method.

This is not true. Constructors should not call overridable methods of their own class. It's fine to call a public method if that method is final or if the class is final.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right; I was too terse. Constructors should also not call any other method that calls an overridable method, either directly or indirectly. It is interesting to note that many Swing components violate this advise - the constructors tend to call setFont() and setBackground().
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you don't want pubic setXXX methods. Then you would do something like this:-
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a specific set of values for the field, that sounds like the prime candidate for an enum.

Can you change the field from String to an enumerated type?

 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and welcome to the Ranch
I hadn't noticed you are new here.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!