Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP 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: 4568
9
  • 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: 20605
60
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