aspose file tools*
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
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: 4490
    
    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: 19785
    
  20

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Determine if enum belongs in a category