aspose file tools*
The moose likes Java in General and the fly likes yet not clear about what is the actual use of enumerated types ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "yet not clear about what is the actual use of enumerated types ?" Watch "yet not clear about what is the actual use of enumerated types ?" New topic
Author

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

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

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 ??




The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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:



"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4465
    
    8

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

Joined: Jul 03, 2011
Posts: 692

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

Joined: Apr 06, 2010
Posts: 4465
    
    8

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

Joined: Jan 10, 2002
Posts: 61654
    
  67

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

Joined: Sep 20, 2010
Posts: 3649
    
  17

I agree. I use them all over.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

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

Joined: Apr 06, 2010
Posts: 4465
    
    8

You can add any methods you want. Here's a very simple example (that also involves giving the enum a constructor):


 
It is sorta covered in the JavaRanch Style Guide.
 
subject: yet not clear about what is the actual use of enumerated types ?