File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to make sure a variable only takes on certain values

 
Bernd Stransky
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
Michael Morris
Ranch Hand
Posts: 3451
  • 0
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64185
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64185
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64185
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.P.S. Welcome to the Ranch!
 
Bernd Stransky
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your ideas.
Bernd
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Joel McNary
Bartender
Posts: 1824
Eclipse IDE Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • 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....
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic