Weird Question
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?
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
Java doesn't support creating variable names dynamically.
destin
Junior Poster in Training
94 posts since Mar 2006
Reputation Points: 32
Solved Threads: 10
> 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:
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
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
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.
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
If you have two hundred buttons you should store them in an array.
destin
Junior Poster in Training
94 posts since Mar 2006
Reputation Points: 32
Solved Threads: 10
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.
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
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:
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.
destin
Junior Poster in Training
94 posts since Mar 2006
Reputation Points: 32
Solved Threads: 10
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.
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
Then I was correct in my last response.
Again, in Ruby, what you just described would look like this:
button_name = "button1"
eval(button_name).whatever # calls whatever() on the variable button1
destin
Junior Poster in Training
94 posts since Mar 2006
Reputation Points: 32
Solved Threads: 10
> It just lets me know about certain properties of an Object, such as its classname
It does much more than that.
> The concept of a Map is somewhat familiar, but it doesn't help avoid the work, does it?
It provides a solution to your problem. IMO, what you are looking for is not a solution but a syntactic sugary way of doing things. Like previously mentioned, either use an array, a map or a JVM targeted scripting language like maybe Groovy, Scala, Rhino etc.
> I guess this isn't possible in Java though.
Yes, in the same way it isn't possible to create classes in C.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
I don't know all that much on reflection, but I have used it before. I don't see how it is useful. I've seen plenty of examples of reflection, so I understand how it is used. Just not why. Can anyone provide a reasonable example of a situation that needs reflection and a short explanation of why? All the tutorials I see give examples of reflection, but they are never situations where reflection is necessary. For example, I saw reflection being used to print out hello world. Pointless.
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
Think of all the frameworks which let you specify your custom class in their configuration file and load those classes at runtime. The servlet specification for instance has a deployment descriptor which allows the developer to configure servlets, filters for his application. All the developer has to do is to specify the class name and the required processing is done at runtime by the container.
Also look into Spring or Guice which are IOC/Dependency Injection frameworks.
That being said, there *are* a lot of uses of reflection; it's just that they aren't that obvious.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
If you make your object Serializable then you can store information about the object in String form. It won't be as readable as "germanShepard" but you could store objects serials somewhere to enter. Doesn't sound too practical.
Another thing you can do is make a factory class. The class would take certain strings and generate objects for you to use based on that string name. It's a common design pattern and I've seen it used while poking around with code at work.
s.o.s, no offense, but sifting through a ton of material to *maybe* answer my question doesn't sound too appealing. If reflection is indeed useful, there have to be some practical, simple explanations of why this is the case.
bionic: Making objects Serializable means you can write the objects to file in Binary form and read them back in this way, so that you can store and retrieve your Objects more easily than if you were to do text input and output. Maybe I misunderstood what you said, but it's actually much more practical than reading/writing Strings to a file.
Regarding your other point, you can also take Strings and make objects by using a simple if statement, so I don't see why that would be a good use of reflection.
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
> Can anyone provide a reasonable example of a situation that needs reflection and a short explanation of why?
We use it on our current project for dynamic configuration of our UI. All panel components share an interface and with a simple XML file we can reconfigure the frames and panels with no change to the compiled jar file. We also use it for our reporting engine to load column classes that can be plugged in to our table-based reports. These are simple usages of loading classes that share a common interface by supplying their class name and other properties in simple xml files.
Ezzaral
Posting Genius
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847