I'm converting some python code to java, and have a situation where I need to call methods of an object but don't know which methods until runtime. In python I resolve this by using getattr on my object and passing it a string that is the name of my method. How would you do something similar in Java?
6 Answers
Class.getField
is your friend. It probably won't be very straightforward though since Python is dynamically typed and Java is statically typed (unless you know the types of your fields in advance.)
EDIT: How to translate these examples. http://effbot.org/zone/python-getattr.htm
Attribute Lookup
Python
//normal
value = obj.attribute
//runtime
value = getattr(obj, "attribute")
Java
//normal
value = obj.attribute;
//runtime
value = obj.getClass().getField("attribute").get(obj);
Method Call
Python
//normal
result = obj.method(args)
//runtime
func = getattr(obj, "method")
result = func(args)
Java
//normal
result = obj.method(args);
//runtime
Method func = obj.getClass().getMethod("method", Object[].class);
result = func.invoke(obj, args);
In the simpler cases, you need to know whether you have a field or a method. esp as they can have the same name. Additionally methods can be overloaded, so you need to know which method signature you want.
If you don't care which method or field you get, you can implement this as a helper method fairly easily.
-
Field.get() returns an Object of any data type. Its not staticly checked. – Peter Lawrey Dec 09 '10 at 13:01
-
3@Peter: Same with `getattr`; Python developers aren't afraid of the unknown :-) – Aaron Digulla Dec 09 '10 at 13:02
-
@Peter Not sure what your point is. In Python you can do `getattr(obj,"field")*2` and that works with ints, floats, strings, lists etc. Trying doing that in Java. – NPE Dec 09 '10 at 13:03
-
You cannot do i*2 where i is an unknown type in Java. I don't see how this is related how you might translate getattr() in Java. – Peter Lawrey Dec 09 '10 at 13:55
-
@NPE Your comment is a little misleading. You can't always use `getattr(obj, "field")*2`. You must know that the `field` attribute implements `__mul__` or `__rmul__`, Python's interface for the multiplication operator. Most things do implement this or inherit some reasonable default, but you can certainly implement classes which either do not implement multiplication, or which do not allow an integer to appear as the right operand. So you very much can still run into type errors like that in Python. The difference is that you catch them at run-time, not compile time, for better or worse. – ely Oct 17 '14 at 14:05
In Java you do this with the Reflection API (and it's usually pretty cumbersome).
MethodUtils
in Apache Commons BeanUtils project may make it a bit easier to work with, though it's a pretty hefty dependency for something simple like this.

- 8,999
- 5
- 36
- 43
You should use the Reflection API. Since the pure API is a bit ... unapproachable, you should have a look at helpers like commons beanutils or reflections.

- 321,842
- 108
- 597
- 820
The easiest way to handle this is to create a Map object in Java class & keep adding the name value pairs & retrieve it accordingly though it might not support different types that setAttr supports.

- 188
- 2
- 12