0

Possible Duplicate:
how to call a java method using a variable name?

Is it possible to use a string as a variable name or method name in Java ?

I am looking for something similar to this in PERL:

$vName = "abc"; $abc = "Hello"; print $$vName;

above code prints Hello

Can I do something similar in Java ?

Community
  • 1
  • 1
JohnH
  • 158
  • 1
  • 11
  • 2
    What you mean like `$methname = "frob"; $obj->$methname($args)`? That very simple and common operation in Perl requires a ridiculous amount of counterintuitive monkeywork in Java. See reflection on Wikipedia. – tchrist Apr 13 '11 at 19:16
  • Take a look at [java reflection](http://download.oracle.com/javase/6/docs/api/java/lang/reflect/package-summary.html). Though I wouldn't call it similar to Perl. – khachik Apr 13 '11 at 19:16
  • 2
    Many questions have already been answered here on SO; please use the search before asking, you might find the answer instantly. – Brian Roach Apr 13 '11 at 19:18
  • 2
    Also this is very bad "Perl". If you had used strict you would get "Can't use string ("abc") as a SCALAR ref while "strict refs" in use". This kind of magic should only be used when necessary. Better would be `$abc = "Hello"; $vName = \$abc; print $$vName;` – Joel Berger Apr 13 '11 at 19:46

0 Answers0