Mazzy
Mazzy

Reputation: 14227

compare To object enum

I have to create a method which tests if two object enum are equal.

Here is the code:

public Passenger{
     private String name_pass;
     public enum StatePass{
          b,c,p
     };
     private StatePass state;

     public Passenger(String name_pass,StatePass state){
            this.name_pass=name_pass;
            this.state=state;
     }

     public boolean isConfirmed(){
          if()
              return true;

          return false;      
     }
}  

Inside the if() I have to check if the field state is equals to p.

How can I do that?

Upvotes: 0

Views: 231

Answers (3)

Has QUIT--Anony-Mousse
Has QUIT--Anony-Mousse

Reputation: 77505

try using state.equals(StatePass.p). Where is your problem?

Upvotes: 0

Oleg Mikheev
Oleg Mikheev

Reputation: 17444

You can use both equals() and == to compare Java enums, so you may choose from

if(StatePass.p.equals(state))

or

if(StatePass.p == state)

Look here for more info

Upvotes: 2

Robin
Robin

Reputation: 36621

You can compare enum values using ==, so your if can become

if( StatePass.p == state )

Note: in the constructor your second parameter must be a StateClass and not a State or you can never call this.state=state

Upvotes: 1

Related Questions