Reputation: 13933
Example class:
class Test {
public int marks;
public String location;
public String show(){
return "Good morning!";
}
}
Is there a way I can access marks
and locations
by a getter? Maybe like so:
Test t = new Test();
System.out.println(t.get("marks"));
and maybe
System.out.println(t.call("show"));
The exact use case I have is to be able to access R.id.[fieldname] for accessing Android Resource ID
Can this work ?
R.id.getClass().getField("date").get(R.id) ?
How could I do this ?
Upvotes: 1
Views: 79
Reputation: 2066
To invoke a method and get its value from some other class use this
Method method = obj.getClass().getMethod("Methodname", new Class[] {});
String output = (String) method.invoke(obj, new Object[] {});
Upvotes: 2
Reputation:
Test t = new Test();
System.out.println(t.getClass().getField("marks").get(t));
Upvotes: 3