aspose file tools*
The moose likes Beginning Java and the fly likes how to make sure a variable only takes on certain values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to make sure a variable only takes on certain values" Watch "how to make sure a variable only takes on certain values" New topic
Author

how to make sure a variable only takes on certain values

Bernd Stransky
Ranch Hand

Joined: Nov 20, 2001
Posts: 47
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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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!


[Jess in Action][AskingGoodQuestions]
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
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.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

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

Joined: Jan 10, 2002
Posts: 61206
    
  66

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

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

Joined: Jan 10, 2002
Posts: 61206
    
  66

P.P.S. Welcome to the Ranch!
Bernd Stransky
Ranch Hand

Joined: Nov 20, 2001
Posts: 47
Thanks for your ideas.
Bernd
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

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

Joined: Aug 20, 2001
Posts: 1817

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....


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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...


"I'm not back." - Bill Harding, Twister
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
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
 
subject: how to make sure a variable only takes on certain values