getting the variable name

If I want to merge two variables and want to get the value of the resultant variable(String) for eg.

String var1 = "ab";

String var2 = "bc";

String abbc = "hello";

I have the two variables var1 and var2. On mergeing the two variables the resultant string is "abbc". I want the value of Variable abbc from this. Is it possible.

[358 byte] By [abhishekva] at [2007-11-27 1:30:46]
# 1
One just should not think of doing such things in a programming language.Unless you are thinking about a Map's keys: http://java.sun.com/javase/6/docs/api/java/util/Map.htmlWhat are you trying to do?What is your goal? Your context?
DrLaszloJamfa at 2007-7-12 0:32:33 > top of Java-index,Java Essentials,Java Programming...
# 2
if it s possible, i think you might use reflexivity
calvino_inda at 2007-7-12 0:32:33 > top of Java-index,Java Essentials,Java Programming...
# 3
> One just should not think of doing such> things in a programming language.Unless that language is Lisp...
tsitha at 2007-7-12 0:32:33 > top of Java-index,Java Essentials,Java Programming...
# 4

I need to use this code again and again in my programs for the same conditions. I have multiple variables named in a fixed pattern and I want to pick up the value of the variable which is derived after joining multiple variables. So, either I have to put a if else if condition in all the places or put it in a common library, which i do not want to do. So just thought that there should be some mechanism thru which this can be achieved, like eval in javascript etc.

abhishekva at 2007-7-12 0:32:33 > top of Java-index,Java Essentials,Java Programming...
# 5

> I need to use this code again and again in my

> programs for the same conditions. I have multiple

> variables named in a fixed pattern and I want to pick

> up the value of the variable which is derived after

> joining multiple variables. So, either I have to put

> a if else if condition in all the places or put it in

> a common library, which i do not want to do. So just

> thought that there should be some mechanism thru

> which this can be achieved, like eval in javascript

> etc.

As mentioned you should look into using a Map with String keys if you really want to do this. If you want to go even further over the top, use something like beanshell, Groovy or Javascript (evidently made easy in Java 1.6)

Or redesign so you don't have to do this.

Good Luck

Lee

tsitha at 2007-7-12 0:32:33 > top of Java-index,Java Essentials,Java Programming...
# 6

If you have a lot of variables, that's bad:

Object foo1;

Object foo2;

...

Object foo1024;

You should be storing that data in an array or collection.

I suggest you read the collection framework tutorial: http://java.sun.com/docs/books/tutorial/collections/index.html

DrLaszloJamfa at 2007-7-12 0:32:33 > top of Java-index,Java Essentials,Java Programming...
# 7
with the reflection API you can do this:Field field = this.getClass().getDeclaredField("abbc");
Speroa at 2007-7-12 0:32:33 > top of Java-index,Java Essentials,Java Programming...
# 8
Okay, reflection *can* achieve it, but should you be egging the OP on?
DrLaszloJamfa at 2007-7-12 0:32:33 > top of Java-index,Java Essentials,Java Programming...
# 9
> Okay, reflection *can* achieve it, but should you be> egging the OP on?oooh - oooh - I bet you could dream up some crazy AOP solution to this! (or maybe just a perl script you run over the source and recompile on the fly...)
tsitha at 2007-7-12 0:32:33 > top of Java-index,Java Essentials,Java Programming...
# 10
hey, I'm the freedom loving type. If he wants to shoot himself in the foot I'm not going to stop him.
Speroa at 2007-7-12 0:32:33 > top of Java-index,Java Essentials,Java Programming...
# 11
What was it Bjarne wrote about C++? "It's easy to shoot yourself in thefoot writing C code. That's harder to do in C++, but when you do, you blowyour whole foot off!:
DrLaszloJamfa at 2007-7-12 0:32:33 > top of Java-index,Java Essentials,Java Programming...
# 12
hehe..and for those of us who remember the Java 1.1 days; In Java you can set yourself up to be shot in the foot really fast but it doesn't end up happening because the bullet goes too slow.
Speroa at 2007-7-12 0:32:33 > top of Java-index,Java Essentials,Java Programming...