• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

What is Enums?

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

I need some help regarding enums in Java 5.
I understand how to use enums. But my real question is exactly where to use enums ? What is practical application of enums?

Please put some light on it.

Regards,
Deepak
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are times when you want to define constants to express something. For example seasons : winter, spring, summer, fall. Before Java5, you could use constants like :

You could use these constants to check which seasons it is :


You can still do it that way, but Java5 introduced enums to make these constants more OOPish (and also to do like other languages). The cool thing is that an enum is much more than a dull constant. But its purpose is still the same : to define some constants, instead of hard coding values into the source.
 
Deepak Kadam
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Christophe,

Thanks for your reply.


Regards,
Ashok
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Christophe Verre:
You can still do it that way, but Java5 introduced enums to make these constants more OOPish (and also to do like other languages). The cool thing is that an enum is much more than a dull constant. But its purpose is still the same : to define some constants, instead of hard coding values into the source.


Not only more "OOPish" - one of the main reasons for enums is to make sets of constants more type-safe.

The old way, using a bunch of integers is not at all safe:

You could easily assign an illegal value to a variable which is supposed to contain only the value of one of the constants:

If you use an enum, then you can't easily assign an invalid value:
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's part of the OOPish plan, isn't it ? Not to assign bananas to kangaroos
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic