File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Why Reflection api is required? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why Reflection api is required?" Watch "Why Reflection api is required?" New topic

Why Reflection api is required?

Niranjan Prasad
Ranch Hand

Joined: Aug 13, 2005
Posts: 62
Why we require Reflection api?

I want to the usage
Sathya Prabhu

Joined: Mar 10, 2006
Posts: 12

The Reflection API allows Java code to examine classes and objects at run time. The new reflection classes allow you to call another class's methods dynamically at run time.

Another practical use is that many times you are not able to tell exactly which class will be instantiated, until the runtime. In these situations you want to be able to create an object through some identifier such as the name of the class. This is one of the abilities that Reflection API of Java provides.

For more information use this link.

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46349
Reflection allows you toget details from an object whose class you don't know. You can get its name, method details, fields, constructors, and print them on screen.
We are having a discussion on the Advanced Java forum at the moment. See if you can understand it. The "second version of the go() method" will print out class name, and a list of methods, on screen. I made a mistkae with what I posted; the serialize() method should be changed so the "try" block reads like this:-and similarly the "try" block in the go() method should be changed like this:-

The reason for the "finally" blocks is so as to make sure you get the ObjectInputStream and ObjectOutputStream properly closed, even if something goes wrong and there is an exception in the inner "try" blocks. Otherwise you leave them open until you exit Java, which can lead to the file being unavailable for other applications, or even a memory leak.

Reflection is particularly useful to people who are writing IDEs and similar apps; they can analyse the class in development and print out lists of methods. The diagrams you see on IDEs where the methods are in different colours are made using a similar technique.

You will see, on the "advanced" thread I mentioned, that the discussion is about whether it is possible to de-serialize or analyse a class whose original programming one can't get at, and so far we haven't found a way to do it.

Reflection is not the sort of thing you ought to be discussing on a beginner's forum. See whether you can understand this part of the Java tutorial about it.

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Frameworks and tools make use of reflection more often than normal business code. For example to make a tool to generate XML from any object you can use reflection to get a list of public fields and get their values, or get a list of getXXX() methods and call them.

Then you might write another million lines of business code dealing with customers and airplane reservations and pineapples without another line of reflection.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: Why Reflection api is required?
It's not a secret anymore!