This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Help with understanding reflection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Help with understanding reflection" Watch "Help with understanding reflection" New topic
Author

Help with understanding reflection

Shridhar Raghavan
Ranch Hand

Joined: May 27, 2010
Posts: 71



Hi. As you can see i am trying to understand how reflection works.

Can someone help with me out with the process "reflection" or any "Java application startup" goes through?

Have been reading the docs at webpage to understand the jvm process. The docs out there explain reflection and not the jvm process of "class loading" which i think plays a crucial role before reflection kicks in.

Any help to find appropriate docs or explanation of how a jvm starts up an application would be appreciated.

Ciao.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

I'm not sure exactly what you want to know - are you not clear about what the concept of reflection is, or are you just asking what it is useful for?

Normally, when you write a Java program, and you use classes that are for example in libraries (including the standard Java API), the compiler has to know about those classes - you have to have the class files for all those classes somewhere (in a directory or JAR file) and they have to be on the classpath or somewhere else so that the compiler can find them.

Suppose that you are writing a system that can be extended via plug-ins. There are many real systems which have this ability - for example, IDEs such as Eclipse and NetBeans have a plug-in architecture.

Your system needs to be able to load plug-ins and call methods in the classes of a plug-in. But note that those plug-ins are not available when you are writing your system, so you can't write your code so that you can directly call methods in plug-ins. In fact, it's most likely that the plug-ins won't even exist yet when you are writing your system, and you don't even know the class names of the classes in the plug-ins.

What you could then do is define an interface for plug-ins, for example something like this:

And then require that people who write plug-ins implement that interface.

In your system, you would use reflection to load a plug-in dynamically. You could get the name of the class that implements the PlugIn interface from a configuration file. You can then load that class with Class.forName(className); and use reflection to call the methods.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Shridhar Raghavan
Ranch Hand

Joined: May 27, 2010
Posts: 71
Is it safe to say that the purpose of reflection is to allow you to create dynamic applications using a plug-in architecture?

Also is reflection then used only for creating instances at runtime and then invoking methods? Can it be used to modify the behavior of a method at runtime.

What i mean to ask is can i use reflection to change the order of method calls
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Yes, though this would tie whatever you are writing very much into the implementation of whatever you are reflecting over. Not a very safe thing to do. For example, if you call a private method via reflection that relies on state initialized in the method that accesses it you'll get unexpected behaviour.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with understanding reflection
 
Similar Threads
new instance
Accessing method of one application from another
Threads and global variable prolems
Finding the caller of a class
Building a reference to a String