• Post Reply Bookmark Topic Watch Topic
  • New Topic

when to use Enum  RSS feed

 
sam liya
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i read that enum in java is use for when we need fixed set of Constants.But why can't we use array or Collection.
Why specially enums?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But why can't we use array or Collection.


How is an array or collection going to provide a fixed set of constants? Or are you suggesting a way to hold a fixed set of constants?

Henry
 
sam liya
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why can't we put fixed set of Constant to Collection?
if we already know what are the Constant that we need what is the problem?
We can make generic type collection and put them?
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aruna,

Effective Java, 2nd Edition has a good discussion about the use of enums vs. the old style use of static final int constants.

John.
 
Tim-Ting Chang
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
aruna sameera wrote:why can't we put fixed set of Constant to Collection?
if we already know what are the Constant that we need what is the problem?
We can make generic type collection and put them?


Yes but you have to hard-code the relationship in multiple inheritant classes. Use enum to declare a set of constraint values are quicker. I think that's the idea.

we can compute c++ with c += 1 or even c = c + 1, but c++ stays because it's less typing. (no pun intended)
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
aruna sameera wrote:why can't we put fixed set of Constant to Collection?
if we already know what are the Constant that we need what is the problem?
We can make generic type collection and put them?


With enums, it will prevent you from assigning a value the is not in the fixed set. And this is done at the compiler level. All you are proposing is that these constants be stored somewhere.

Prior to the introduction of enums, Java applications used to do this by creating a list of final static variables that have int values. It looked like enums (when you used them), but it didn't really have any of the type safety of one. This is the same with your proposal.

Henry
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Plus you can compare enums using ==, and they can have their own methods. Enums can be quite handy.
 
sam liya
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks All
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!