Is it possible to get a variable name in Java at runtime?
Possible use case: in statements. Given a type statement
assert percval >= 0 && percval <= 100 : "percval out of range: " + percval;
Depending on your IDE, if you reorganize the code, rename the variable, it cannot automatically change the entries in the lines. Possible result:
assert percentage >= 0 && percentage <= 100 : "percval out of range: " + percentage;
It would be nice if something like this:
assert percval >= 0 && percval <= 100 : nameOf(percval) + " out of range: " + percval;
Possible?
source
share