0
String getName(Object o)
{
  //TODO
}
String object = "i want to get "object" not these words"
getName(object);

Is it possible the method return "object"? thx

M. Abbas
  • 6,409
  • 4
  • 33
  • 46
user2539662
  • 130
  • 2
  • 10
  • 2
    Not any variable, only class fields and, through many hardships, parameter names. – Sotirios Delimanolis Sep 17 '13 at 15:18
  • 1
    Why? There's probably a better way to accomplish whatever you're trying to do. – Mike G Sep 17 '13 at 15:19
  • 5
    We're confronting another case of [the XY problem](http://meta.stackexchange.com/q/66377/182862). Please describe your real problem instead of the proposed solution you think it could do – Luiggi Mendoza Sep 17 '13 at 15:20
  • 1
    You know what the variable name is when writing the source. Just use that. – millimoose Sep 17 '13 at 15:20
  • Also, remember a variable only holds a reference to an object. When you pass it to a method like `getName()` you are passing a copy of the value of the reference, so the variable name is lost. – Sotirios Delimanolis Sep 17 '13 at 15:20
  • If you preserve local variable names when compiling, you can retrieve them using the debugging API. See [this question](http://stackoverflow.com/questions/3228770/java-access-to-local-variable-names). – Russell Zahniser Sep 17 '13 at 15:24

1 Answers1

4

There is no way to get the name of a local variable used to pass a value to a method.

You may be able to do some kind of static source code analysis, but at runtime the stack does not include information such as this.

Bohemian
  • 412,405
  • 93
  • 575
  • 722