Thursday, March 22, 2018

java - Get Enum instance




I have an Enum:




public enum Type {

ADMIN(1),
HIRER(2),
EMPLOYEE(3);

private final int id;

Type(int id){
this.id = id;

}

public int getId() {
return id;
}
}


How can I get a Type enum passing an id property?


Answer




Try this out. I have created a method which searches type using id:



public static Type getType(int id) {

for (Type type : Type.values()) {
if (id == type.getId()) {
return type;
}
}
return null;

}

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...