I have several interfaces all with the same constants – ID and ROOT. I also have a method into which I pass an object that will be an implementation of one of these interfaces.
How can I dynamically retrieve the value of the constant depending on the class passed in – i.e. I want to do something like the following:
public void indexRootNode(Node node, Class rootNodeClass)
{
indexService.index(node, rootNodeClass.getConstant('ID'),
rootNodeClass.getConstant('ROOT'));
}
In PHP this is easy, but is this possible in Java? I’ve seen this problem solved using accessors on the constant, but I want to retrieve the constant directly. Annotations won’t help me here either.
Thanks
This can be achieved using reflection (also see corresponding javadoc).
Maybe you may additionaly have to cast the values to the corresponding type.