2

I have several calls to Enum DogStatus in my code, like this:

if(DogStatus.SLEEPING){
    //do something
}

How can I set up a constant of the Enum in my class, so that I don't need to alter all of them each time I want to change the if condition?

E.g. something like:

constant statusToCheck = DogStatus.SLEEPING 
java123999
  • 6,974
  • 36
  • 77
  • 121

1 Answers1

2
private final DogStatus statusToCheck = DogStatus.SLEEPING;

or, if statusToCheck is declared inside a method:

final DogStatus statusToCheck = DogStatus.SLEEPING;

Java enums are immutable object instances and can be referenced as any other objects.

andrucz
  • 1,971
  • 2
  • 18
  • 30