Weird Question

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2008
Posts: 1,658
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Weird Question

 
0
  #1
Nov 8th, 2008
Is there any way that I can take a String and somehow use it as if its the name of one of my declared Objects? For example, if I have an Animal named germanShephard, is there some way I could say String gerSh = "germanShephard"; and then treat gerSh as if it was the equivalent of saying germanShephard in my program?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 254
Reputation: Antenka has a spectacular aura about Antenka has a spectacular aura about Antenka has a spectacular aura about 
Solved Threads: 65
Antenka's Avatar
Antenka Antenka is offline Offline
Posting Whiz in Training

Re: Weird Question

 
0
  #2
Nov 8th, 2008
Beforehand I'm sorry for my English...
I don't know any way to dynamically create variable, but I can offer some alternatives:
1. Use Dictionary
2. Create class variable dynamicly
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 94
Reputation: destin is an unknown quantity at this point 
Solved Threads: 10
destin's Avatar
destin destin is offline Offline
Junior Poster in Training

Re: Weird Question

 
0
  #3
Nov 8th, 2008
Java doesn't support creating variable names dynamically.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Weird Question

 
0
  #4
Nov 8th, 2008
Originally Posted by BestJewSinceJC View Post
Is there any way that I can take a String and somehow use it as if its the name of one of my declared Objects? For example, if I have an Animal named germanShephard, is there some way I could say String gerSh = "germanShephard"; and then treat gerSh as if it was the equivalent of saying germanShephard in my program?
Use either TreeMap or HashMap

Dictionary is also an okay way to go, but I believe it is a deprecated API (or incredibly outdated).

Edit: Actually, you can probably get away with using a Pair<K, V> instead of something that handles an entire collection.

I'm not sure where it is defined in the Java Standard Library, but if all else fails you can easily create your own pair class.
Last edited by Alex Edwards; Nov 8th, 2008 at 9:03 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,653
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Weird Question

 
0
  #5
Nov 9th, 2008
> For example, if I have an Animal named germanShephard, is there some way I could say
> String gerSh = "germanShephard"; and then treat gerSh as if it was the equivalent of
> saying germanShephard in my program?

You can't because gerSh is now a String. The closest you can come to this kind of name to Object mapping is by using a Map or by using the reflection API, which again doesn't work for `private' members.

The behavior you speak of can be seen in all ECMAScript implementations. Something of the sort:
  1. var obj = {dog: {name: "poo boy", type: "poodle"},
  2. cat: {name: "neko san", type: "siamese"}};
  3. var key = "dog";
  4. alert(obj.dog.type); // poodle
  5. alert(obj[key].type); // poodle
The romantic image of an über-programmer is someone who fires up Emacs, types like a machine gun, and delivers a flawless final product from scratch. A more accurate image would be someone who stares quietly into space for a few minutes and then says “Hmm. I think I’ve seen something like this before.” - John D
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,658
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Weird Question

 
0
  #6
Nov 9th, 2008
I've used reflection before but I don't understand how it helps solve my problem. It just lets me know about certain properties of an Object, such as its classname. The concept of a Map is somewhat familiar, but it doesn't help avoid the work, does it? The primary reason I'd want to do this is b/c it'd be less work. For example, if I had 200 buttons, button1-button200, it'd be useful if I could do what I described in my first post. I guess this isn't possible in Java though.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 94
Reputation: destin is an unknown quantity at this point 
Solved Threads: 10
destin's Avatar
destin destin is offline Offline
Junior Poster in Training

Re: Weird Question

 
0
  #7
Nov 9th, 2008
If you have two hundred buttons you should store them in an array.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,658
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Weird Question

 
0
  #8
Nov 9th, 2008
I agree, but that isn't what I asked. Anyway, thanks for your help everybody. If anyone knows any languages where you can do this, please let me know, with a code example if you would. Otherwise consider this topic closed.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 94
Reputation: destin is an unknown quantity at this point 
Solved Threads: 10
destin's Avatar
destin destin is offline Offline
Junior Poster in Training

Re: Weird Question

 
0
  #9
Nov 9th, 2008
I still don't understand the question entirely, but in many scripting languages you can evaluate any given string as if it were code.

Example in Ruby:
  1. var = "Hello, world!"
  2. puts var # prints: Hello, world!
  3.  
  4. var2 = "var"
  5. puts eval(var2) # prints: Hello, world!

Anyway, except when metaprogramming, this technique should be used very sparingly. There aren't many good reasons to do this.

And sorry if I misinterpreted your question.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,658
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Weird Question

 
0
  #10
Nov 10th, 2008
I think you answered my question correctly in your first post. I was just asking if it is possible to save a value into a String, then use that String as if it was the name of an Object (rather than being a String).

So, for example, if I had a String buttonName = "button1"; then I was asking if I could do

buttonName.whatever(), and have buttonName evaluate itself to see whether or not there was an Object named "button1" in the program. If so, it would call whatever().

Now obviously, this doesn't work as I wrote it, since Java treats buttonName as a String (as it should). I was just asking if there were any techniques that would do what I described above.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 1751 | Replies: 28
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC