java - Retrieving variables with the same name as a string -
i have bunch of maps inside class , make generic method retreive specific map matches id. like:
private map map1; private map map2; private map map3; public map getmap(string id){ return mapx; }
and when call method as:
getmap("map1");
it return map1. way can conversion whithout creating map of maps?
try use reflection, example:
private map getmap(string id) throws nosuchfieldexception, illegalaccessexception { getclass().getfield("map" + id).get(this); return map; }
Comments
Post a Comment