Thursday 16 May 2019

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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...