Java - True/False per Enum value

enums, java

Solution

That's not really how an `enum` works. You wouldn't include the `boolean` flag, but instead do this:

public enum Status {
   INPATIENT,
   OUTPATIENT,
   EMERGENCY;
}

public class Patient {
    private Status status;

    public void setStatus(final Status status) {
        this.status = status;
    }
}

public class SomeService {
     public void someMethod(final Patient patient) {
         patient.setStatus(Status.INPATIENT);
         patient.setStatus(Status.OUTPATIENT);
         patient.setStatus(Status.EMERGENCY);
    }
}

A variable typed as an `enum` can hold any one value of that `enum` (or `null`). If you want to change status, change which value of the enum the variable refers to. (`Enum`s are different from `class`es, since they are not instantiated with the `new` keyword, but rather just referenced directly, as in the above code.)

Problem

I'm trying to figure out what is the best solution for my problem. I have an object where the status may be one of three possibilities but it can change during run time. I have three status flags that the object can be. I have no experience with `ENUM` and trying to figure out if this is the best way. I want to be able to set a specific flag to `true` or `false` and then be able to set another one. I need to be able to get the status of each flag as well for when I iterate through a list of these objects within a array list. ``` class Patient { //REST OF the object public enum Status { INPATIENT(false), OUTPATIENT(false), EMERGENCY(false); private final boolean isStatus; Status(boolean isStatus) { this.isStatus = isStatus; } public boolean isStatus() { return this.isStatus; } } } ```

Original source