• 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

how to make sure a variable only takes on certain values

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I have a String variable that is supposed to only have certain values, e.g.
"up"
"down"
"unknown"
etc.
I can certain check in my method if the String parameter passed by the object caller has one of these values.
Question: How can I enforce that the caller only can set the value to one of the required ones?
Any info is appreciated,
Thanks
Bernd
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't, really. What you can do is define a
to represent only the legal values:
public class Direction {
// Private constructor is important!
private Direction() {}
public static final Direction UP = new Direction();
public static final Direction DOWN = new Direction();
// etc.
}

Now you redefine your method to accept a Direction as an argument; the callers can only pass in one of the defined public static values!
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also enforce it using Exceptions. For example:

You may decide to define your own Exception since IllegalArgumentException is not a checked Exception. Ordinarily though you prefer a RuntimeException in these situations since it generally indicates an error in program logic.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The OP said

I can certain check in my method if the String parameter passed by the object caller has one of these values.


and wanted to know how to get static checking instead.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The OP said ...


This is true, but many times posters ask for something without realizing that there might be alternative ways to do things, or that a way they thought wasn't optimal may actually be the accepted practice.
I don't think pointing out such alternatives is a bad thing. Do you?
bear
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
P.S. That said, I would also have suggested the Type-Safe Enum pattern as a possible solution, but you beat me to it!
bear
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
P.P.S. Welcome to the Ranch!
 
Bernd Stransky
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your ideas.
Bernd
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bear:

I don't think pointing out such alternatives is a bad thing. Do you?


No, not at all. Sorry, I didn't mean to sound rude.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to throw in my $.02, Java 1.5 will provide the ability to declare enumerations. This doesn't help you now, but it would down the road....
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually it can help you now if you install the pre-release generics compiler frm Sun. Though it's a bit of a pain to use, and doesn't work well with IDEs (who don't know about the new features). And if you try to do serious development with it, there's a significant risk that the eventual release will not behave exactly the same, so you may get in trouble. But you can certainly play around with it...
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
The OP said

and wanted to know how to get static checking instead.


Dr. Friedman-Hill,
First, let me say that it is an honor to have someone of your caliber answering questions here at JavaRanch. Second, I meant no disrespect nor did I mean to suggest that your solution was not optimal, but as Bear mentioned sometimes poster haven't considered other possibilites.
I would like to ask a favor of you. Would you start a thread in JIG Advanced on rule based systems. That is a subject that I and I know many others here are interested in, yet I know very little about the theory. As soon as your book is available I plan to get a copy but it would be nice to know a little bit in advance.
 
God is a comedian playing for an audience that is afraid to laugh - Voltair. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic