• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Real use case example for reflection in java

 
Nishanth Hampali
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

See http://www.jdbc-tutorial.com/.

Cheers,
Ramon
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

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.

Thanks
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic