• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

What is Java Reflection

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to know what exactly is Reflection facility in Java.
I read on several blogs regarding Java Reflection but I got the same answer at all the places that said "Java's Reflection API's makes it possible to inspect classes, interfaces, fields and methods at runtime, without knowing the names of the classes, methods etc. at compile time". But I didn't get what is the use of this facility?
Can anybody guide me on this?
 
author & internet detective
Posts: 42003
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As an example, suppose I want to be able to log all the fields of an object. Any object. With reflection, I can write a method that looks up all the fields of the object and calls a method to get their values. Without reflection, I would have to code this log method for every object I wanted to log.
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

MaheshS Kumbhar wrote:I want to know what exactly is Reflection facility in Java.
I read on several blogs regarding Java Reflection but I got the same answer at all the places that said "Java's Reflection API's makes it possible to inspect classes, interfaces, fields and methods at runtime, without knowing the names of the classes, methods etc. at compile time". But I didn't get what is the use of this facility?
Can anybody guide me on this?



It would help if you explain what you didn't get. The statement that you provided is pretty self explanatory.

Henry
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is Reflection?
A group of methods, starting in the classes java.lang.Class and java.lang.ClassLoader used to dynamically find Java Classes and execute code on them. It provides mechanisms for finding the classes, instantiating instances of the class, locating and executing methods, and accessing data members.

Why would you use Reflection?
There are a host of reasons. Some examples:
- Dynamic 'Drop Ins': You want to be able to expand your application by simply dropping in JARs or Classes into the class path after the application has been deployed. This means the 'main' part of the application doesn't know anything about what classes are available at run time, and so can't reference them directly. Reflection can be used to locate and load these drop-ins, then automatically execute code on the drop in which would 'register' it with the main application - making it available for use. This is common in a lot of open source image manipulation applications where users can write drop ins for custom analysis, or for importing various image formats. It can be used in games to provide user-generated levels, etc...

- Mapping 'events' to 'actions'. This is very common in situations where an application has very distinct actions to perform based on some dynamic event. The event occurs and an action is supposed to happen. But there can be lots of events and they can change over time. You define each event and each action as its own class, and then provide some mapping between them. But because the events can change you don't want to have to modify the application each time the event occurs. So you create the classes for events and actions, then create a configuration file to define what events can occur and how to map those events to their corresponding actions. Your main app would read the configuration file and use Reflection to generate the instances of the Events and Actions, and to map them appropriately. Now when you add new events, retire old ones, change what actions need to occur, etc... you no longer have to re-compile your entire application. You just put the new .class files in and change a config file. For examples, look at Servlet Engines like Tomcat, or any JEE server.

- It is sometimes nice to have a simple to use script type interface to present to the world, when your users aren't programmers. This might include auto-completion or hints, drop-down lists to suggest values, variables, or methods... On the back end you want normal Java objects to make it easy to interact with the rest of your application. Reflection can be used to find all the available classes, methods and members of those classes so you can present them to the user. This is how JavaBeans and the Introspector works, and is used by some GUI building tools and some JEE tools like the JSP EL markup.
 
MaheshS Kumbhar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A very thanks Steve Luke for your detailed explaination. Now I understood what is reflection along with situations where reflection mechanism is used.
Thanks once again.
 
Marshal
Posts: 79969
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

MaheshS Kumbhar wrote:A very thanks Steve Luke . . .

Yes, that was a good explanation. As with most things, you will find about it in the Java™ Tutorials.
 
You know it is dark times when the trees riot. I think this tiny ad is their leader:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic