• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reflection  RSS feed

 
Arpit Sharma
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the advantage of reflection apart from importing the files in the java file .

suppose
-------------
package test;

// import java.util.ArrayList
Class A {

public static void main( String []a ){

// dont go for syntax ...
System.out.print(Class.forName("java.util.ArrayList").newInstance());


}
}
-----------
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
https://java.sun.com/docs/books/tutorial/reflect/index.html
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reflection is best used for when you don't know the exact implementation that will be used until runtime. Take for example:

Two implementation of a List Generator

Both of these implementations would do the same job, but do it in different ways. I could choose which method to use when I run the application using a configuration file or command line argument:


Since you can use configuration files, environment properties, or command line properties to define what specific class to use then your running code doesn't have any dependency on the specific class, and you will be able to generate new implementations without affecting your current code. This makes it useful for frameworks, for plug-in capability, and for application extensions.

One API that uses it a lot is the JDBC API where each database needs its own driver for talking with the database. Since Java can't know about all possible implementations they allow the user to choose which implementation should be used by providing a JAR with the proper driver and using reflection to register the driver for use.

Servlet Containers use it as well. You create a configuration file (web.xml) which defines which Servlets are part of the web application. The servlet container then goes through the list using reflection to load the classes so that it can instantiate and initialize them, and finally use them for serving requests.

Reflection can also be used to find a lot of information about a class or object, as well as to dynamically load and instantiate them. You have the ability to lookup method names, variables, annotations, and the class heirarchy.

The Reflection Tutorial for more.
See
 
Arpit Sharma
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks steve
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another example(though not advisable to use) of the power of reflection:



Output:
 
Arpit Sharma
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks satya
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!