Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

when to use Enum

 
Ranch Hand
Posts: 1325
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
shawn peter
Ranch Hand
Posts: 1325
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Plus you can compare enums using ==, and they can have their own methods. Enums can be quite handy.
 
shawn peter
Ranch Hand
Posts: 1325
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks All
 
Grow a forest with seedballs and this tiny ad:
The trailboss has a kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic