Accessing string representations of constant field values in java -
i using imported class has constant field values set using:
public static final int blue = 1; public static final int green = 2;
etc.
is there way of getting string representation of constant field value int value?
i.e. given value 2 want string of green.
p.s. isn't class can't use enums
if can change class contains these constants, better make enum value()
method.
otherwise, suggest building map<integer, string>
once using reflection, , doing map lookups:
map<integer, string> valuetostringmap = new hashmap<>(); (field field : foo.class.getfields()) { int modifiers = field.getmodifiers(); if (field.gettype() == integer.class && modifier.ispublic(modifiers) && modifier.isstatic(modifiers)) { valuetostringmap.put((integer) field.get(null), field.getname()); } }
Comments
Post a Comment