Monday, August 4, 2008

Java Reflection -- Myths and Reality

I thought to share my experience with you all people about java Reflection. People have many issues about using reflection some people consider that Reflection creates the performance overhead, some feel that we need more code to write to achieve the desired result by using reflection etc etc. These are some myths which are around the development community.

Today I thought to explain the reality apart from myths and 1000 people benchmark testing applications. So lets start exploring Reflection...

question is what is the Reflection : it is an API and the best way to reference and object class without any compilation error because in all other methods we have to import the class physically at compile time.

The reflection class may be Unknown at compile time or Dynamically loaded at runtime

Reflection allows programs to interrogate an object at runtime without knowing the object's class.

there are four myths about Reflection around the world and that are :

1) Reflection is only useful for JavaBeans technology based components
2) Reflection is too complex for use in general purpose applications
3) Reflection reduces performance of applications
4) Reflection cannot be used with 100% pure Java certification standard

What I found from hard core developers around the world and by reading many articles and books and also from my own experiences the answers of each and every questions is as follows

for point 1) Reflection is only useful for JavaBeans technology based components

The first answer is NO because people have wrong understanding of Reflection as they believe that it can be only used with JavaBeans technology based components as some other pure object oriented languages are also using Reflection such as Smalltalk and Eiffel.

Rather it has more benitis like Reflection helps to keep software robust and make application more Flexible, Extensible and Pluggable.

for point 2) Reflection is too complex for use in general purpose applications

The first answer for this point is also NO. According to me Reflection makes the code simpler and helps us in removing useless nested if/else statements. The skills required to use Reflection can easily be mastered. Reflection can significantly reduce the footprint of an application and imporve reusability.

for point 3) Reflection reduces performance of applications

heheh again the abrupt answer is NO and the statement is False, Reflection can actually increase the performance of code according to my experience and my experience is also proven by eclipse RCP engine as it is using Reflection heavily. Lets see how it can improve the performance. the main factor which can contribute to increase the performance are :

a) By Reducing and removing expensive conditional code
b) By Simplifying source code and design
c) and also can greatly expand the capabilities of the application

for point 4) Reflection cannot be used with 100% pure Java certification standard

According to the experts the answer should be NO for this point also as this statement is also proven false. There are only few restrictions :
"the program must limit invocations to classes that are part of the program or part of the JRE"

Now I will explain you that why we have to use Reflection :). Reflection solves the problems withing object-oriented design :

• Flexibility
• Extensibility
• Pluggability


Reflection solves the problems caused by ...

• The static nature of the class hierarchy
• The complexities of strong typing


Reflection also can benefits the Design Patterns and can decouple the object further and by doing this can simplify and reduce maintenance.

The best Design Pattern used with the Reflection are the Observer
and Command.

This topic and discussion is not meant to write the Reflection apis :) and explaining the Reflection in detail. for that purpose we can write one more post in forum :D hehe to get the number one position :D hehehe...

The main purpose for this post was to remove some myths which is spread in the community and again re-spreading by the other people who never used the reflection.

Cheers and enjoy using reflection

No comments: