Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enumns - Pros/Cons?

 
Marcus Hathaway
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody,

I wondered if somebody could explin the benefits/purpose of using Enums?
I've also heard you need to be careful with them also..?

Any thoughts would greatly be appreciated.
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read this article
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nigel,
I checked the article which you have provided but unable to understand what exactly is the int enum pattern?


In prior releases, the standard way to represent an enumerated type was the int Enum pattern:
// int Enum Pattern - has severe problems!
public static final int SEASON_WINTER = 0;
public static final int SEASON_SPRING = 1;
public static final int SEASON_SUMMER = 2;
public static final int SEASON_FALL = 3;


Thanks in advance

Naseem
 
Vladimir Nesov
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means that standard way of implementing enums was as you quoted, that is using integer constants.
Note that typesafe enum pattern was still not significantly more difficult, and enum keyword is just some syntactic sugar upon such pattern.
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It also provides for some rather interesting and powerful functionality to have a full firstclass type as an enumeration rather than a primitive.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Enums are a great way to implement simple Strategy classes with a fixed set of implementations.

The only drawback of enums is that they can't inherit from other classes (because they already implicitely inherit from the enum base class)..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic