943,787 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2129
  • Java RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Nov 10th, 2008
0

Re: Weird Question

Then I was correct in my last response.

Again, in Ruby, what you just described would look like this:
Ruby Syntax (Toggle Plain Text)
  1. button_name = "button1"
  2. eval(button_name).whatever # calls whatever() on the variable button1
Reputation Points: 32
Solved Threads: 10
Junior Poster in Training
destin is offline Offline
94 posts
since Mar 2006
Nov 10th, 2008
0

Re: Weird Question

> 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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Nov 10th, 2008
0

Re: Weird Question

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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 10th, 2008
0

Re: Weird Question

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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Nov 10th, 2008
0

Re: Weird Question

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.
Reputation Points: 10
Solved Threads: 6
Newbie Poster
bionicseraph is offline Offline
19 posts
since Nov 2008
Nov 10th, 2008
0

Re: Weird Question

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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 10th, 2008
0

Re: Weird Question

> 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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Nov 10th, 2008
0

Re: Weird Question

Reflection is a very specialized solution, much like the Java Native Interface, Remote-Method Invocation, Wildcards, Design Patterns, transient values, volatile values, concurrency, etc.

Don't expect simple solutions for something that is seemingly "simple."
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Nov 10th, 2008
0

Re: Weird Question

BestJew: Yeah, I meant an if statement. Reflection is probably overkill for your stated requirements.
Reputation Points: 10
Solved Threads: 6
Newbie Poster
bionicseraph is offline Offline
19 posts
since Nov 2008
Nov 10th, 2008
0

Re: Weird Question

Click to Expand / Collapse  Quote originally posted by Ezzaral ...
> 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.
I've reflection to make method calls on auto-generated DAO objects that were based on similar tables in my data base. There are good uses for reflection, but most of the time you can get by without using it.
Reputation Points: 10
Solved Threads: 6
Newbie Poster
bionicseraph is offline Offline
19 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Printing output of an array
Next Thread in Java Forum Timeline: How would you go about coding this?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC