How can I return a property with String name of it inside a class?
My pseudo code is:
class Foo {
public static final String test = "test";
public static String bar(String name) {
return foo.getProperty(name);
}
}
UPDATE
I have a class with "setting" name , and this class have a method with getSetting() name , this method get a string key and if a property exist with this key, return the property , if no , search in database for this key,it's nod good solution?
Try Java Reflection - Fields with #getFields();
In your scenario, you can try:
public String getSetting(String strKey){
Class tmp = setting.class;
try {
Field field = tmp.getField(strKey);
return value; // depend on your class instance or field type.
} catch (NoSuchFieldException e) {
// get data from database and return
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.