• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the usage on enum types?

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the usage on enum types and where they are useful?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64719
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anywhere you to abstract a finite set of values. A classic example is card suits:

The primary advantages are code readability and type-safety.
 
Ernie Mcracken
Ranch Hand
Posts: 33
Mac Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you know all the possible states of what you are describing in your code when you compile it then you should use enumerated types. Simples.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12101
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A (smallish) finite set of things is always a good candidate. As Bear's example illustrates, it's much easier to read and understand code that says

if (card.getSuit().equals(HEARTS))

than code that says

if (card.getSuit() == 2)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic