| | |
Weird Question
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 1,658
Reputation:
Solved Threads: 206
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?
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
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
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
•
•
•
•
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?
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.
> 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:
> 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:
Java Syntax (Toggle Plain Text)
var obj = {dog: {name: "poo boy", type: "poodle"}, cat: {name: "neko san", type: "siamese"}}; var key = "dog"; alert(obj.dog.type); // poodle 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
•
•
Join Date: Sep 2008
Posts: 1,658
Reputation:
Solved Threads: 206
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.
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:
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.
Example in Ruby:
Ruby Syntax (Toggle Plain Text)
var = "Hello, world!" puts var # prints: Hello, world! var2 = "var" 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.
•
•
Join Date: Sep 2008
Posts: 1,658
Reputation:
Solved Threads: 206
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.
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.
![]() |
Similar Threads
- driver_irql error question (Windows NT / 2000 / XP)
- Straight C (C++)
- HAELP! Fixed vundo and now have no desktop. (Viruses, Spyware and other Nasties)
- Access Control Exception vs. Applet Viewer (Java)
- Quick directory question (C#)
- system restarts by itself...and more weird things (Windows NT / 2000 / XP)
- Weird, weird problems with fstream (C++)
- IE "can't find server" after loading home page (Viruses, Spyware and other Nasties)
- Uber-N00B question (C++)
- What is BRIDGE.DLL (Viruses, Spyware and other Nasties)
Other Threads in the Java Forum
- Previous Thread: Printing output of an array
- Next Thread: How would you go about coding this?
Views: 1751 | Replies: 28
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application arguments array arrays automation bidirectional binary birt bluetooth calculator chat class classes client code columns component database designadrawingapplicationusingjavajslider detection draw eclipse editor error errors event exception expand file fractal game givemetehcodez graphics gui guidancer helpwithhomework html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jmf jni jpanel julia linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie number object oracle os plazmic print problem program programming project recursion scanner screen server set signing size smart sms smsspam socket sort sql string subclass support swing test threads time transfer tree windows






