Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

Reflection, how to understand

Thread view: 
John_Woo - 31 Aug 2006 18:11 GMT
Hi,

I'm trying understand jdk 5 reflection.

I know it's for loading/invoking method on the fly. I'm wondering, any
simple core-java example (not sort of EJB, rmi) illuestrated how an app
re-compile then invoke the newly classes/methods, and why need to do
that (recompile)?

--
Thanks lots.
John
Toronto
Oliver Wong - 31 Aug 2006 19:04 GMT
> Hi,
>
[quoted text clipped - 4 lines]
> re-compile then invoke the newly classes/methods, and why need to do
> that (recompile)?

   Reflection isn't nescessarily related to recompilations. I think the
name "reflection" is supposed to convey the image of a Java program looking
at itself in a mirror, or in other words, to look at itself.

   For example, you might have an object which is an instance of Class. You
can query this object to find out what methods it has, and invoke these
methods. This would be using reflection, even if no (re)compilation was
involved.

   - Oliver
John_Woo - 31 Aug 2006 19:42 GMT
> > Hi,
> >
[quoted text clipped - 13 lines]
> methods. This would be using reflection, even if no (re)compilation was
> involved.

Thanks, Oliver.

if I want to see how a class recompiled and its method updated then
invoked, do you have an example, and what cases need a class to be
compiled in running time?

John
Oliver Wong - 31 Aug 2006 19:53 GMT
> if I want to see how a class recompiled and its method updated then
> invoked, do you have an example,

   There's some documentation at
http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/javac.html#proginterface
but I've never tried doing this myself.

> and what cases need a class to be
> compiled in running time?

   Almost none. In a Turing-Computable sense, anything you could do by
recompiling and dynamically loading, you could also do by interpreting and
emulating.

   - Oliver
jagonzal@gmail.com - 31 Aug 2006 19:45 GMT
> Hi,
>
[quoted text clipped - 4 lines]
> re-compile then invoke the newly classes/methods, and why need to do
> that (recompile)?

AFAIK, it doesn't have to do with recompiling - it has to do with, as
you said, dynamically accessing to classes.

For example:

String classname = "org.some.class.TestClass";
Object[] parameters;
Object obj =
Class.forName(classname).getConstructor(Object[].class).newInstance(new
Object[] { parameters });

What this code snippet does is:

Get the class named "org.some.class.TestClass".
(Class.forName(classname)).
Then it proceeds to get a constructor for said class, one that requires
an object array as parameter ( getConstructor(Object[].class)).
Then it proceeds to instantiate the object using said constructor,
passing the Object array "parameters" as the parameters.

There you have it - I have loaded an instance of the class
org.some.class.TestClass - without having to know that name in the code
- the String classname could have been loaded from any source (in
particular, I use that code snippet for persistence of simple objects
from a database - load the className and the constructor parameters
from the database, execute the reflection voodoo to instantiate)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.