• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Restricting the field of a class to certain values

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 732
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I will probably just use this code directly in the constructor.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe you don't want pubic setXXX methods. Then you would do something like this:-
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...and welcome to the Ranch
I hadn't noticed you are new here.
 
reply
    Bookmark Topic Watch Topic
  • New Topic