• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to check value at compile time  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code throws an exception at runtime because there are not enough bottles in the fridge (for a weekend with a BBQ with friends 5 bottles of beer are probably not enough but more than 50 bottles don't fit into the fridge and may lead to a bad hangover). How can i get that code to do the value check at compile time (fail if the amount of beer is beyond limits as in my example)


[ September 23, 2007: Message edited by: Michael Miller ]
 
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael,
The only way I can think of to check this at compile time is horribly inefficient in terms of human time: Create a enum with each of the valid values and point to these. The downsides are that it is tedious, inflexible (try changing the valid limits) and callers can no longer do math with it.

What problem are you trying to solve by moving the check to compile time? Maybe there is a better solution to this problem.
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You'll have to add the main() yourself: It will have to catch the throw in the constructor. May I suggest takinig the number of beer bottles requested from the command line ? Then you would have a useable tool that could be taken to Ranch activities.

 
Michael Miller
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is not a real problem i have, it is only a question i had to answer in an exam. and even though the exam is over i still would like to know how to do it.

You for example can't do something like:

because the compiler complains: integer number too large.

how can i get that? don't use an int but use a MyNumberOfBottles in the constructor (but how does the MyNumberOfBottles class look like?)?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Miller:
it is not a real problem i have, it is only a question i had to answer in an exam. and even though the exam is over i still would like to know how to do it.

That's good. Because the implementation doesn't work well in practice. One answer is what I posted above - define a constant for each allowable value.
 
Michael Miller
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First i wan't to thank for your previous responses.

I think, i forgot to mention that a hint i was given was to use a special kind (class?) of number which guarantees, that if an instance of it is passed to the fridge constructor, it is already clear, that it is a valid value (or is that exactly your suggestion Jeanne and i just don't really understand it?).

I thought about using some static stuff in the MyNumber class? Or can i implement an interface or something like that to get it done?

I'm sorry to still bother you, but i'm still looking for the solution i think it was asked for (but the question was quite vague). So if you have got any other ideas, please let me know.
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne is suggesting you use enums; see here. Instead of accepting a number as a constructor argument, you'd accept only an instance of an enum class whose values corresponded to the legal values of the number. Then the compiler would only let you pass those values.

As Jeanne said, in the case where there are nearly 50 different legal values, this would be quite tedious.
 
Michael Miller
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, took a while, but maybe, i got it now . Is there also any possible solution for that problem in java versions before 1.4?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Miller:
Ok, took a while, but maybe, i got it now . Is there also any possible solution for that problem in java versions before 1.4?

Yes, but it's more awkward. You can create a class to wrap the integer value.



If you make MyClass(int) a private constuctor, you can ensure there are only legal values in it. All of these approaches are convoluted and should be avoided.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeanne Boyarsky:

If you make MyClass(int) a private constuctor, you can ensure there are only legal values in it.


Just to point out (in case it's not obvious to Michael) this is really all that Java enums are -- a little bit of language support for exactly what Jeanne has described here.
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can read more about typesafe enumerations (pre-JDK 5) here (see "Replace Enums with Classes", where Enum refers to the C language keyword, not the Java one) or, for even more info, in the book Effective Java. The book is strongly recommended for other reasons, but for enumerated types nowadays it's much easier to use JDK 5 enums. And come to think of it, it probably makes more sense now to wait until the second edition comes out, supposedly in November. So, never mind.
 
Michael Miller
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all. Your answers have helped me much. Seems to me the solution Jeanne mentioned (the one with the class with a private constructor was the one which was looked for. And, if i understood that right, from Java 5 upwards that solution is naturally supported as Enumeration just does that stuff).

So, thanks again. I think, the sleepless nights are over now
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!