• 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

Why enum keyword in Java 5?

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been learning about the new enum keyword in Java 5. I'm curious what you can accomplish with the new enum keyword that you coudln't accmoplish before with regular Java objects.

For example, you create an Enum class that does the following:

- Contains a Map that associates String keys with Integer values.
- Contains a private int that contains the "current" value of the Enum in instances of the Enum class.
- Restricts outside access to the Map.
- Only allows keys and values to be set in the class when it is constructed.
- Throws an exception if someone attempts to set an invalid value as the value of an Enum instance.

I've read that enum keyword gives increased type safety in Java, at least compared to using variables in a class definition, but if you used a class like the one I just described I don't think this is the case. You couldn't ever set an incorrect value in the class described above, and methods could be written to accept instances of the class, not raw ints.

I know the guys (and gals) that design the Java programming language are a lot smarter than me. I must be missing some great advantage of the enum keyword. It just seems odd to add that amount of complexity to the language for something that could be acheived with regular classes.

Can someone explain the design logic/advantages of the enum keyword to me?

Thanks,

Landon
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As more recent changes in Java, it was not strictly necessary to add enums, but merely convenient.
The existing solutions were as you indicated not very save (constants), or tedious to implement correctly (classes). Moreover, they do exist in several other languages, so developers are used to have them available.
For this readon,it was decided to add them to the language, and then also allow them to be used in switch cases, which can not be done using classes.

Compare for example to the enhanced for loop, also not really necessary, but convenient to have.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This article may get you started.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you could do exactly the same things before Java 5 - it was just more work, you simply had to write more code. With other words, it's pure syntactic sugar - quite sweet sugar, I would add.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic