Possible Duplicate:
Java Reflection: How to get the name of a variable?
I've found various discussions on this topic but never any satisfying answer..
A anyVariableName = new A();
How can I get the name of the variable anyVariableName
and get string "anyVariableName"
as a result?
Is this possible in Java?
If anyVariableName
is a local variable, you can't. If it's a field, use Class#getField()
or Class#getDeclaredField()
.
The only places local variable names are stored is in the source (which you can parse) or the debug information (which you can get from the byte code if it has been included)
If it's a field you can use Class.getDeclaredFields() if you don't know the name.