This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Determine if enum belongs in a category

 
John Vorwald
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4567
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Vorwald
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic