使用Java反射,可以获得局部变量的名称吗?例如,如果我有以下内容:
Foo b = new Foo();
Foo a = new Foo();
Foo r = new Foo();
有没有可能实现一种方法,可以找到这些变量的名称,比如:
public void baz(Foo... foos)
{
for (Foo foo: foos) {
// Print the name of each foo - b, a, and r
System.out.println(***);
}
}
EDIT: This question is different from 100 in that it more purely asks the question about whether one can use reflection to determine the name of a local variable, whereas the other question (including the accepted answer) is more focused on testing values of variables.