This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Java in General and the fly likes Real use case example for reflection in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Real use case example for reflection in java" Watch "Real use case example for reflection in java" New topic

Real use case example for reflection in java

Nishanth Hampali

Joined: Dec 07, 2010
Posts: 1
Hi All,

I was going through the reflection concept in java.
From the tutorial i could understand that it allows the running java program to get to know its internal properties..

Now i could not think any real examples for this scenario. Can you please help me with one such real example?

Thanks in advance
Ramon Anger
Ranch Hand

Joined: Apr 19, 2011
Posts: 56

Hi Nishanth,

the simplest example I could think of is registering a JDBC database driver by using Class.forName(String className) before you can establish a database connection.



Blackbelt on
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

Every component-container mechanism is built using reflection. Examples are plenty - Servlet container, EJB container, Spring IoC container, even Struts and all externally configured web frameworks.

Basically, whenever we configure a Java object in XML (or any external medium) and try to set any properties on it from config itself, reflection is in the picture. Though it is hidden from application programmers and is implemented in the container itself.
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1341


Posting one real case scenario where java reflection can be handy.

For eg - finding details of java class of which you have no source code access. only .class file is present with you.

There you can use java reflection code which can take fully qualified class name and print all its instance variables and methods signature. In this way you will be able to get some details around that class.

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575
I agree. Here's the link:
subject: Real use case example for reflection in java
It's not a secret anymore!