αƞjiβ
αƞjiβ

Reputation: 3266

Comparing enum in Java

I have a DTO with enum type. I wan to test DTO field but not sure how can I do it.

Class MyDto {
  public enum ExamResult { PASS, FAIL };
  private ExamResult result;
  ...
}

I am trying to test the result field in some other class

if(myDto.result.equals("PASS")) {
  //doSomething
} else {
  //doOtherthing
}

But .equals() always returing me false even it have correct value. So not sure how can I test it.

Upvotes: 0

Views: 12552

Answers (3)

Rolf ツ
Rolf ツ

Reputation: 8801

When comparing for equality Enums values act as if they are integers. There-for you should use the == operator. Although equals(ExamResult.PASS) also works fine as mentioned in the comments (But this is not very often used).

if(myDto.result == ExamResult.PASS) {
  //Do something
} else {
  //Do other thing
}

If you only have access to a String representation of the Enum value you could use this type of code: (But I don't recommend it)

if(myDto.result.getName().equals("PASS")) {
  //Do something
} else {
  //Do other thing
}

More explanation: Enum values are given a unique integer value at compile time. In the Java language Enum values are guaranteed to be unique in name and compile time number. You can get an Enum unique number using the ordinal() method. The values of Enums can be get as an array representation using the values() method. The name of an Enum value can be get as a String representation using the name() method.

Upvotes: 4

Costi Ciudatu
Costi Ciudatu

Reputation: 38265

Java enums can declare their own variables and methods (which can be overridden by individual constants).

Besides the options listed in @AndyThomas' answer, you may consider moving the actual logic associated with each constant within the constant itself (think about the strategy pattern).

This allows you to reuse that logic in other places in your application if needed, without the need for auxiliary utility methods, in a pure OOP style. Here's a pretty complex example of what you can do (just to get an idea about what's possible):

enum ExamResult {
    PASS(true) {
        // override the default behavior to do something else
        @Override public void doSomething() {
            super.doSomething();
            System.out.println("whatever");
        }
    },
    FAIL(false);

    private final boolean success;

    ExamResult(boolean success) { this.success = success }

    // you can declare this as abstract if there's no default behavior
    public void doSomething() {
        System.out.println("Success: " + success);
    }
}

Now, your code simply becomes:

myDto.result.doSomething();  // of course you need to make sure result is not null

If you have a String holding the exam result:

ExamResult.valueOf(yourString).soSomething();

Upvotes: 2

Andy Thomas
Andy Thomas

Reputation: 86509

You may test for equality with the == operator. Although the enum constants are objects, there is exactly one instance of each.

JLS 8.9: Enum constants: Because there is only one instance of each enum constant, it is permitted to use the == operator in place of the equals method when comparing two object references if it is known that at least one of them refers to an enum constant.

    if ( ExamResult.PASS == myDto.result ) {
         // Do something
    }

You may also test for equality with the equals() method. (Using the constant on the left avoids a NullPointerException if the variable is null.)

    if ( ExamResult.PASS.equals( myDto.result )) {
         // Do something
    }

You may also compare against the name of an enum constant, which is accessed through the name() method.

    if ( myDto.result.name().equals( "PASS")) {
         // Do something
    }

Finally, you can also use enums in switch statements. With more than two enum constants, this may be more concise.

    switch ( myDto.result ) {
    case PASS:
        // Do something
        break;

    case FAIL:
    default:
        // Do something
        break;
    }

Upvotes: 2

Related Questions