• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Enums over constants

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have heard that we should use enums rather than contants. Can somebody please explain me the advantages of using enums over constants?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like most things, it depends on the situation.

If you just have a small number of scalar values that you don't want to use magic numbers for, and in particular if they're not related, then constants are fine:


However, if you want several related values, and especially if they're more than just simple scalars--that is, if they're objects with behavior or more than one value in their state--then an enum is appropriate. Look at the source code for java.util.concurrent.TimeUnit for a good example of this.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anand Jain wrote:I have heard that we should use enums rather than contants. Can somebody please explain me the advantages of using enums over constants?


I think what you may have heard is that you should use enums over primitive constants.

The main reason for this is that an enum constant can't be mistaken for something else, whereas an (eg) int value can. Enums also have several useful structures, such as EnumSet for comparing sets of constants.

HIH

Winston
 
Anand Jain
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Anand Jain wrote:I have heard that we should use enums rather than contants. Can somebody please explain me the advantages of using enums over constants?


I think what you may have heard is that you should use enums over primitive constants.

The main reason for this is that an enum constant can't be mistaken for something else, whereas an (eg) int value can. Enums also have several useful structures, such as EnumSet for comparing sets of constants.

HIH

Winston



Hi..Thanks for the reply..do you mean to say enums provide type safety which primitive constants don't provide?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Anand Jain wrote:I have heard that we should use enums rather than contants. Can somebody please explain me the advantages of using enums over constants?


I think what you may have heard is that you should use enums over primitive constants.

The main reason for this is that an enum constant can't be mistaken for something else, whereas an (eg) int value can.



Actually, that's another case I missed, and it applies not just to primitives, but to Strings as well.

If you have a fixed number of valid values that are known at compile time, such as Jan, Feb, etc. for month names or left, right, up, down for directions--then enums are useful for mapping those inputs to valid typed objects at input time and then using the objects throughout the rest of your code.

For example, instead of


if we have a Direction enum, then we can do


We know that that method can only ever be called with a valid value. Whereas if we just had String constants (or even int constants, 1 for up, 2 for down, etc.), then we could easily pass any arbitrary value that's not valid for our method.


By using enums, we ensure that if an inappropriate value is passed, it's a compile-time error, whereas the above won't get detected until runtime. Compile-time errors are always easier to find and fix than runtime errors.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anand Jain wrote:

Hi..Thanks for the reply..do you mean to say enums provide type safety which primitive constants don't provide?



I'm not sure if that's what he means (though I think it is), but it's definitely true that they do.

 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:Actually, that's another case I missed, and it applies not just to primitives, but to Strings as well.


Very true. I'm an old C programmer (which is where, I believe, the term 'enum' originated), so I'm well aware of the perils.

The classic one for me is java.awt.Color, which contains a pile of numeric constants for colours. Totally inappropriate use, and potentially dangerous; unfortunately now pretty much part of the language. Maybe they'll get around to deprecating it some day...

Winston

[Edit] Oops. Forget the above. Color is just fine. Calendar on the other hand...
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anand Jain wrote:Hi..Thanks for the reply..do you mean to say enums provide type safety which primitive constants don't provide?


Yup. They also provide the facility to embed behaviour (ie, code), so for example a Suit enum for a deck of cards might be able to rank them by seniority for a particular game.

Winsotn
 
reply
    Bookmark Topic Watch Topic
  • New Topic