Monday, 11 September 2017

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

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...