java - How to retrieve Enum name using the id? -


i have enum as:

public enum enumstatus {      passed(40l, "has passed"),     average(60l, "has average marks"),     good(80l, "has marks");      private java.lang.string name;      private java.lang.long id;      enumstatus(long id, java.lang.string name) {         this.name = name;         this.id = id;     }      public java.lang.string getname() {         return name;     }      public java.lang.long getid() {         return id;     } } 

i have enum names(passed, average, good) using ids only(40,60, 80). how do it?

create static method in enum searches in values (implicit method/member, don't know it) , returns corresponding value. cases in method can not find matching value, should create special entry, e.g. unknown, can return. way, not have return null, bad idea.

public static enumstatus getbyid(long id) {     for(enumstatus e : values()) {         if(e.id.equals(id)) return e;     }     return unknown; } 

btw - code seems wrong. bracket after good seems not belong there.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -