This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Determine if enum belongs in a category Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Determine if enum belongs in a category" Watch "Determine if enum belongs in a category" New topic
Author

Determine if enum belongs in a category

John Vorwald
Ranch Hand

Joined: Sep 26, 2010
Posts: 139
I'm using enum to represent the state / status. Generally, the state / status can be fit into one of the following categories "can execute", "can not execute", or "unknown". I would like to have a function that determine if the enum can not be executed. Is the following a good solution?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

I think it's a better approach to put the flag inside the enum and initialise it with a constructor. That way you avoid the big boolean statement, and it doesn't get more complicated if you add more options. So something like this:

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

And even if you would want to check for constants (Matthew's solution is a lot better though), why not compare on instances?
At least you wouldn't be able to make a typo like this.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
John Vorwald
Ranch Hand

Joined: Sep 26, 2010
Posts: 139
Matt, that is a better approach.
If I have

Is the constructor called with false and true, respectively? I'm not familiar with this approach to calling the constructor.

Rob, that is a good suggestion too. I didn't know how to to use the this variable for an enum.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Determine if enum belongs in a category
 
Similar Threads
Copy a Class
howto avoid this complexity (state pattern?)
To set tool tip only for the leaf nodes
modifying enums
Java 5 enum - How to display a different value?