This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

yet not clear about what is the actual use of enumerated types ?

 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the basic enumerated program i was trying to learn , but i yet not clear about what is the actual use of enumerated types ?
i mean when and where we have to use enumerated types ??


 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your implementation of the enum is flawed. Show(String name) and Show(String names, int Num) aren't constructors.
Also variable names should start with a lowercase letter. A practical example of an enum:

 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before enums were introduced to Java, the usual approach was to have a bunch of constant values instead. But then, you don't get type safety.

The example from your other thread is better for demonstrating the advantage:
You could have this instead:
Now you can write all your code to use an int to represent the suit. But what happens if someone somewhere sets suit = 0? Or suit = 23? Using an enum completely prevents that.

Alternatively you could "roll your own" enums, and define a class with only four instances to represent the suits. That way you get type safety, but it's more long-winded. enums do it all for you.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Before enums were introduced to Java, the usual approach was to have a bunch of constant values instead. But then, you don't get type safety.

The example from your other thread is better for demonstrating the advantage:
You could have this instead:
Now you can write all your code to use an int to represent the suit. But what happens if someone somewhere sets suit = 0? Or suit = 23? Using an enum completely prevents that.

Alternatively you could "roll your own" enums, and define a class with only four instances to represent the suits. That way you get type safety, but it's more long-winded. enums do it all for you.

thanks , so this is the only use of enum or it has more uses other than this type safe
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naved momin wrote:thanks , so this is the only use of enum or it has more uses other than this type safe

Well, it's useful any time you have a fixed set of values for a quantity (like the four suits). Type-safety is the biggest advantage, in my opinion, but there are other advantages. enums are just classes, which means you can give enums methods and attributes just like any other class - that can be very useful at times.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64973
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote: that can be very useful at times.

Very useful! Extraordinarily useful!

And, enums can also help keep code readable.
 
Stephan van Hulst
Bartender
Pie
Posts: 5898
63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree. I use them all over.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:
naved momin wrote:thanks , so this is the only use of enum or it has more uses other than this type safe

Well, it's useful any time you have a fixed set of values for a quantity (like the four suits). Type-safety is the biggest advantage, in my opinion, but there are other advantages. enums are just classes, which means you can give enums methods and attributes just like any other class - that can be very useful at times.

so is there any thing that the method name should be same to enum type

 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can add any methods you want. Here's a very simple example (that also involves giving the enum a constructor):


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic