• Post Reply Bookmark Topic Watch Topic
  • New Topic

enum equivalent in java  RSS feed

 
Reshma Shanbhag
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
do we have anything like enum as in C++/VB in java.
Thanks in Advance
Reshma
 
Arun Boraiah
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out java.util.Enumeration
 
Reshma Shanbhag
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun,
Enumeration in java.utilis not the same as 'enum' is c++/VB. java.util.Enumeration is used to iterate through the collection of objects which is now replaced with the java.util.Iterator.
Reshma.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Short answer - no, Java does not have this. You can declare a series of public static final int fields to get essentially the same effect - but not automatically as you get with enum. And you should also read about the enumerated type pattern, described here in Chapter 5 of Joshua Bloch's Effective Java. This is much more type-safe than C's enum is - but it takes some effort to set up correctly. It's rumored that the next JDK release may include a simple mechanism for generating enumerated types like this - if so, this will probably be reasonably similar to enum, but with added type-safety.
 
Reshma Shanbhag
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Jim, your reply was very helpful.

reshma
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the moment, Java does not have enumerations. There are a few different ways Java developers tend to go about this: usually they either define numeric constants (public static final short; I'd argue that only the unthoughtful use int), or they use typesafe enumerations using Java classes. See the C2 Wiki for a thorough exploration of all the alternatives.
You will be overjoyed to hear that a genuine enumeration facility will probably be part of the upcoming Java release (v1.5, "Tiger"). These new enums are better than any of their C/C++/C#/Pascal counterparts.
- Peter
PS. Why didn't I see Jim's reply before writing this? Oh well...
[ April 28, 2003: Message edited by: Peter den Haan ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why didn't I see Jim's reply before writing this?
Guess you were too busy tracking down all those links, which I'd been too lazy to do. So some good came of it anyway.
public static final short; I'd argue that only the unthoughtful use int
Mmmm, why's that? I mean, I'd probably use an enumerated type anyway... but what's the big benefit of short instead of int? Storage space maybe? (Why not byte then?) I suppose that may be significant in some cases. Of course, if it is, then the memory used in storing the identifier for each item is probably more significant. Does this lead to "use short names to save memory"? Rarely seems worth worrying about, to me - unless maybe if you're using J2ME. Perhaps I'm overlooking some other effect of short vs. int?
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
Mmmm, why [ do you think "only the unthoughtful use int" ]
It's much more mundane than any of that, Jim. It's a kind of poor man's type safety, based on the observation that "short" is probably the least often used integral type smaller than int. Consider the classThen it's very easy to make the mistakeThis cannot happen if you use short:Of course, it does by no means give you full type safety, but it will alert you to most cases of wrong parameter ordering at no cost.
- Peter
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!