File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes 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 "Java Reflection" Watch "Java Reflection" New topic

Java Reflection

aakash bhatt
Ranch Hand

Joined: Jan 09, 2003
Posts: 182
What is the advantage and disadvantage of using Reflecton ane when exactly should we use it.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
hi aakash,
i've some points in mind.
i'd assume that u already know reflection but strive to figure out its significance.
1. reflection is a heavy thing to use in the sense that we have to go through many things to invoke even a simple method on an object.
2. usually we don't need in our application code.
3. we need reflection when we want to build an application that will be dealing with classes that can be defined by the end user who uses the application and our application needs to know things about those classes.
A) Java BeanBox- BeanBox uses reflection to know things about the Bean and uses it to display information in the Property Sheet etc..
B) Third party applications like "Rule Engine" which needs to allow loading/importing of the user defined classes so that the "Rule Engine" application can be used to define further "Rules" on "our" objects (this might be difficult to grasp as there is a new term "Rule Engine" but that is just an example. it can be any third party application that needs to import/load classes defined by us and allow us to do something more to those classes via that application)
C) what will you do to build a tool that can import any classes and then create a object model diagram from it? we can use reflection to know information about classes and prepare the model. though i don't know issues in this application.
the above 2 kinds are the biggest application of reflection i would say.
also, think how would you know methods/variables in some classes if you were not given any javadoc, api , java source files and you just have .class files which are not obfuscated? of course, we need to use reflection to know methods etc in the classes.

I agree. Here's the link:
subject: Java Reflection
It's not a secret anymore!