File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes What is Java Reflection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "What is Java Reflection" Watch "What is Java Reflection" New topic
Author

What is Java Reflection

MaheshS Kumbhar
Ranch Hand

Joined: Sep 24, 2009
Posts: 188
I want to know what exactly is Reflection facility in Java.
I read on several blogs regarding Java Reflection but I got the same answer at all the places that said "Java's Reflection API's makes it possible to inspect classes, interfaces, fields and methods at runtime, without knowing the names of the classes, methods etc. at compile time". But I didn't get what is the use of this facility?
Can anybody guide me on this?


I am slow but sure
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31069
    
232

As an example, suppose I want to be able to log all the fields of an object. Any object. With reflection, I can write a method that looks up all the fields of the object and calls a method to get their values. Without reflection, I would have to code this log method for every object I wanted to log.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19068
    
  40

MaheshS Kumbhar wrote:I want to know what exactly is Reflection facility in Java.
I read on several blogs regarding Java Reflection but I got the same answer at all the places that said "Java's Reflection API's makes it possible to inspect classes, interfaces, fields and methods at runtime, without knowing the names of the classes, methods etc. at compile time". But I didn't get what is the use of this facility?
Can anybody guide me on this?


It would help if you explain what you didn't get. The statement that you provided is pretty self explanatory.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

What is Reflection?
A group of methods, starting in the classes java.lang.Class and java.lang.ClassLoader used to dynamically find Java Classes and execute code on them. It provides mechanisms for finding the classes, instantiating instances of the class, locating and executing methods, and accessing data members.

Why would you use Reflection?
There are a host of reasons. Some examples:
- Dynamic 'Drop Ins': You want to be able to expand your application by simply dropping in JARs or Classes into the class path after the application has been deployed. This means the 'main' part of the application doesn't know anything about what classes are available at run time, and so can't reference them directly. Reflection can be used to locate and load these drop-ins, then automatically execute code on the drop in which would 'register' it with the main application - making it available for use. This is common in a lot of open source image manipulation applications where users can write drop ins for custom analysis, or for importing various image formats. It can be used in games to provide user-generated levels, etc...

- Mapping 'events' to 'actions'. This is very common in situations where an application has very distinct actions to perform based on some dynamic event. The event occurs and an action is supposed to happen. But there can be lots of events and they can change over time. You define each event and each action as its own class, and then provide some mapping between them. But because the events can change you don't want to have to modify the application each time the event occurs. So you create the classes for events and actions, then create a configuration file to define what events can occur and how to map those events to their corresponding actions. Your main app would read the configuration file and use Reflection to generate the instances of the Events and Actions, and to map them appropriately. Now when you add new events, retire old ones, change what actions need to occur, etc... you no longer have to re-compile your entire application. You just put the new .class files in and change a config file. For examples, look at Servlet Engines like Tomcat, or any JEE server.

- It is sometimes nice to have a simple to use script type interface to present to the world, when your users aren't programmers. This might include auto-completion or hints, drop-down lists to suggest values, variables, or methods... On the back end you want normal Java objects to make it easy to interact with the rest of your application. Reflection can be used to find all the available classes, methods and members of those classes so you can present them to the user. This is how JavaBeans and the Introspector works, and is used by some GUI building tools and some JEE tools like the JSP EL markup.


Steve
MaheshS Kumbhar
Ranch Hand

Joined: Sep 24, 2009
Posts: 188
A very thanks Steve Luke for your detailed explaination. Now I understood what is reflection along with situations where reflection mechanism is used.
Thanks once again.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
MaheshS Kumbhar wrote:A very thanks Steve Luke . . .
Yes, that was a good explanation. As with most things, you will find about it in the Java™ Tutorials.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is Java Reflection