By Lehane


2008-09-01 08:39:21 8 Comments

What is reflection, and why is it useful?

I'm particularly interested in Java, but I assume the principles are the same in any language.

21 comments

@Isuru Jayakantha 2014-11-06 04:42:31

Simple example for reflection. In a chess game, you do not know what will be moved by the user at run time. reflection can be used to call methods which are already implemented at run time:

public class Test {

    public void firstMoveChoice(){
        System.out.println("First Move");
    } 
    public void secondMOveChoice(){
        System.out.println("Second Move");
    }
    public void thirdMoveChoice(){
        System.out.println("Third Move");
    }

    public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { 
        Test test = new Test();
        Method[] method = test.getClass().getMethods();
        //firstMoveChoice
        method[0].invoke(test, null);
        //secondMoveChoice
        method[1].invoke(test, null);
        //thirdMoveChoice
        method[2].invoke(test, null);
    }

}

@human.js 2012-06-22 15:35:55

Example:

Take for example a remote application which gives your application an object which you obtain using their API Methods . Now based on the object you might need to perform some sort of computation .

The provider guarantees that object can be of 3 types and we need to perform computation based on what type of object .

So we might implement in 3 classes each containing a different logic .Obviously the object information is available in runtime so you cannot statically code to perform computation hence reflection is used to instantiate the object of the class that you require to perform the computation based on the object received from the provider .

@Atom 2015-06-22 18:42:11

I need something similar.. An example would help me a lot as I am new to reflection concepts..

@ndm13 2018-01-11 02:09:37

I'm confused: can't you use instanceof to determine object type at runtime?

@cprn 2019-04-26 09:34:35

As I find it best to explain by example and none of the answers seem to do that...

A practical example of using reflections would be a Java Language Server written in Java or a PHP Language Server written in PHP, etc. Language Server gives your IDE abilities like autocomplete, jump to definition, context help, hinting types and more. In order to have all tag names (words that can be autocompleted) to show all the possible matches as you type the Language Server has to inspect everything about the class including doc blocks and private members. For that it needs a reflection of said class.

A different example would be a unit-test of a private method. One way to do so is to create a reflection and change the method's scope to public in the test's set-up phase. Of course one can argue private methods shouldn't be tested directly but that's not the point.

@Ben Williams 2008-09-02 16:15:28

One of my favorite uses of reflection is the below Java dump method. It takes any object as a parameter and uses the Java reflection API to print out every field name and value.

import java.lang.reflect.Array;
import java.lang.reflect.Field;

public static String dump(Object o, int callCount) {
    callCount++;
    StringBuffer tabs = new StringBuffer();
    for (int k = 0; k < callCount; k++) {
        tabs.append("\t");
    }
    StringBuffer buffer = new StringBuffer();
    Class oClass = o.getClass();
    if (oClass.isArray()) {
        buffer.append("\n");
        buffer.append(tabs.toString());
        buffer.append("[");
        for (int i = 0; i < Array.getLength(o); i++) {
            if (i < 0)
                buffer.append(",");
            Object value = Array.get(o, i);
            if (value.getClass().isPrimitive() ||
                    value.getClass() == java.lang.Long.class ||
                    value.getClass() == java.lang.String.class ||
                    value.getClass() == java.lang.Integer.class ||
                    value.getClass() == java.lang.Boolean.class
                    ) {
                buffer.append(value);
            } else {
                buffer.append(dump(value, callCount));
            }
        }
        buffer.append(tabs.toString());
        buffer.append("]\n");
    } else {
        buffer.append("\n");
        buffer.append(tabs.toString());
        buffer.append("{\n");
        while (oClass != null) {
            Field[] fields = oClass.getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                buffer.append(tabs.toString());
                fields[i].setAccessible(true);
                buffer.append(fields[i].getName());
                buffer.append("=");
                try {
                    Object value = fields[i].get(o);
                    if (value != null) {
                        if (value.getClass().isPrimitive() ||
                                value.getClass() == java.lang.Long.class ||
                                value.getClass() == java.lang.String.class ||
                                value.getClass() == java.lang.Integer.class ||
                                value.getClass() == java.lang.Boolean.class
                                ) {
                            buffer.append(value);
                        } else {
                            buffer.append(dump(value, callCount));
                        }
                    }
                } catch (IllegalAccessException e) {
                    buffer.append(e.getMessage());
                }
                buffer.append("\n");
            }
            oClass = oClass.getSuperclass();
        }
        buffer.append(tabs.toString());
        buffer.append("}\n");
    }
    return buffer.toString();
}

@Tom 2010-04-29 13:06:11

What should Callcount be set to?

@Tom 2010-04-29 13:13:21

I got a Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError when I ran this.

@fny 2012-11-23 06:09:31

@Tom callCount should be set to zero. It's value is used to determine how many tabs should precede each line of output: each time dump needs to dump a "subobject", the output would print as nested in the parent. That method proves useful when wrapped in another. Consider printDump(Object obj){ System.out.println(dump(obj, 0)); }.

@Arnaud P 2013-07-01 23:03:02

The java.lang.StackOverflowError might be created in case of circular references, because of the unchecked recursion: buffer.append(dump(value, callCount))

@stolsvik 2013-10-08 10:06:27

Can you specifically release your code to Public Domain, pretty please?

@BSeitkazin 2018-12-05 06:35:57

I want to answer this question by example. First of all Hibernate project uses Reflection API to generate CRUD statements to bridge the chasm between the running application and the persistence store. When things change in the domain, the Hibernate has to know about them to persist them to the data store and vice versa.

Alternatively works Lombok Project. It just injects code at compile time, result in code being inserted into your domain classes. (I think it is OK for getters and setters)

Hibernate chose reflection because it has minimal impact on the build process for an application.

And from Java 7 we have MethodHandles, which works as Reflection API. In projects, to work with loggers we just copy-paste the next code:

Logger LOGGER = Logger.getLogger(MethodHandles.lookup().lookupClass().getName());

Because it is hard to make typo-error in this case.

@Marcus Thornton 2015-12-08 07:02:19

Reflection is to let object to see their appearance. This argument seems nothing to do with reflection. In fact, this is the "self-identify" ability.

Reflection itself is a word for such languages that lack the capability of self-knowledge and self-sensing as Java and C#. Because they do not have the capability of self-knowledge, when we want to observe how it looks like, we must have another thing to reflect on how it looks like. Excellent dynamic languages such as Ruby and Python can perceive the reflection of their own without the help of other individuals. We can say that the object of Java cannot perceive how it looks like without a mirror, which is an object of the reflection class, but an object in Python can perceive it without a mirror. So that's why we need reflection in Java.

@AnthonyJClink 2018-01-30 16:14:22

type(), isinstance(), callable(), dir() and getattr(). .... these are pythonic reflection calls

@saumik gupta 2016-04-26 22:54:49

Reflection gives you the ability to write more generic code. It allows you to create an object at runtime and call its method at runtime. Hence the program can be made highly parameterized. It also allows introspecting the object and class to detect its variables and method exposed to the outer world.

@Desmond Smith 2014-10-17 11:59:33

Uses of Reflection

Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique and can enable applications to perform operations which would otherwise be impossible.

Extensibility Features

An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names. Class Browsers and Visual Development Environments A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code. Debuggers and Test Tools Debuggers need to be able to examine private members in classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to ensure a high level of code coverage in a test suite.

Drawbacks of Reflection

Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.

  • Performance Overhead

Because reflection involves types that are dynamically resolved, certain Java virtual machine optimizations cannot be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts and should be avoided in sections of code which are called frequently in performance-sensitive applications.

  • Security Restrictions

Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.

  • Exposure of Internals

Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.

source: The Reflection API

@Matt Sheppard 2008-09-01 08:44:58

The name reflection is used to describe code which is able to inspect other code in the same system (or itself).

For example, say you have an object of an unknown type in Java, and you would like to call a 'doSomething' method on it if one exists. Java's static typing system isn't really designed to support this unless the object conforms to a known interface, but using reflection, your code can look at the object and find out if it has a method called 'doSomething' and then call it if you want to.

So, to give you a code example of this in Java (imagine the object in question is foo) :

Method method = foo.getClass().getMethod("doSomething", null);
method.invoke(foo, null);

One very common use case in Java is the usage with annotations. JUnit 4, for example, will use reflection to look through your classes for methods tagged with the @Test annotation, and will then call them when running the unit test.

There are some good reflection examples to get you started at http://docs.oracle.com/javase/tutorial/reflect/index.html

And finally, yes, the concepts are pretty much similar in other statically types languages which support reflection (like C#). In dynamically typed languages, the use case described above is less necessary (since the compiler will allow any method to be called on any object, failing at runtime if it does not exist), but the second case of looking for methods which are marked or work in a certain way is still common.

Update from a comment:

The ability to inspect the code in the system and see object types is not reflection, but rather Type Introspection. Reflection is then the ability to make modifications at runtime by making use of introspection. The distinction is necessary here as some languages support introspection, but do not support reflection. One such example is C++

@Krsna Chaitanya 2012-11-23 05:59:37

can u please explain what is the significance of that null parameter in this line Method method = foo.getClass().getMethod("doSomething", null);

@Matt Sheppard 2012-11-27 01:14:19

The null indicates there are no parameters being passed to the foo method. See docs.oracle.com/javase/6/docs/api/java/lang/reflect/…, java.lang.Object...) for details.

@bigtunacan 2013-03-12 03:46:31

Just to clear up since this has so many upvotes. The ability to inspect the code in the system and see object types is not reflection, but rather Type Introspection. Reflection is then the ability to make modifications at runtime by making use of introspection. The distinction is necessary here as some languages support introspection, but do not support reflection. One such example is C++.

@Bill K 2013-07-29 15:50:01

I love reflection but if you have control over the code then using reflection as specified in this answer is unnessary and therefore an abuse--You should use Type Introspection (instanceof) and strong types. If there is any way but reflection to do something, that's how it should be done. Reflection causes serious heartache because you lose all advantages of using a statically typed language. If you need it you need it, however even then I'd consider a pre-packaged solution like Spring or something that completely encapsulates the reflection necessary--IE: let someone else have the headaches.

@geschema 2014-06-19 13:51:03

Isn't introspection the same as self-reflection?

@Neon Warge 2016-01-27 15:04:54

I just realize one thing maybe this is what I am doing in JavaScript? I open up firebugs/chrome inspector plug a break point and see what my code doing on that point, also looking for methods if it exists on an instance and try it out and check what it spits out? All this done on runtime. If this is the case wow, this is what it is.

@RestInPeace 2016-04-29 09:34:26

@bigtunacan Where did you get that information from? I see the term "reflection" used in official Java documentation from Oracle to describe not only the ability to make changes at runtime but also the ability to see the type of an object. Not to mention that most so-called "type introspection" related classes (ex: Method, Constructor, Modifier, Field, Member, basically apparently all except Class) are within the java.lang.*reflect* package. Perhaps the concept "reflection" comprehensively includes both "type introspection" and modification at run-time?

@bigtunacan 2016-04-29 12:53:29

@RestInPeace, that is a correct assumption. Reflection relies on introspection and so encompasses that as a piece. My comment was to clarify that reflection allows runtime modifications. The answer gives an example that shows these runtime modifications occurring, but defines reflection as just the ability to do the introspection. This question has very high SEO for reflection and I thought it important to add additional clarification.

@Sebastian Redl 2017-02-06 09:06:06

C++ does not, at this time, support type introspection in any relevant way. It is planned for a future version. Some external tools (such as Qt's moc) extend C++ with type introspection capabilities.

@Lucas 2017-03-24 13:51:11

C# have dynamic objects and anonymous objects too, like ExpandoObject

@Wesos de Queso 2017-07-30 22:47:47

It useless for non Java editors. It would be insane if you could modify classes attributes or methods at runtime.

@idelvall 2018-06-18 06:54:36

@bigtunacan that is not accurate, at least in the Java world. Reflection is the API to both get and set that information at runtime whereas introspection is the process of inspecting java beans, based on method naming convention and reflection stackoverflow.com/questions/2044446/…

@Mohammed Sarfaraz 2017-04-09 02:29:34

As name itself suggest it reflects what it holds for example class method,etc apart from providing feature to invoke method creating instance dynamically at runtime.

It is used by many frameworks and application under the wood to invoke services without actually knowing the code.

@roottraveller 2017-03-22 07:27:08

Reflection is an API which is used to examine or modify the behaviour of methods, classes, interfaces at runtime.

  1. The required classes for reflection are provided under java.lang.reflect package.
  2. Reflection gives us information about the class to which an object belongs and also the methods of that class which can be executed by using the object.
  3. Through reflection we can invoke methods at runtime irrespective of the access specifier used with them.

The java.lang and java.lang.reflect packages provide classes for java reflection.

Reflection can be used to get information about –

  1. Class The getClass() method is used to get the name of the class to which an object belongs.

  2. Constructors The getConstructors() method is used to get the public constructors of the class to which an object belongs.

  3. Methods The getMethods() method is used to get the public methods of the class to which an objects belongs.

The Reflection API is mainly used in:

IDE (Integrated Development Environment) e.g. Eclipse, MyEclipse, NetBeans etc.
Debugger and Test Tools etc.

Advantages of Using Reflection:

Extensibility Features: An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.

Debugging and testing tools: Debuggers use the property of reflection to examine private members on classes.

Drawbacks:

Performance Overhead: Reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which are called frequently in performance-sensitive applications.

Exposure of Internals: Reflective code breaks abstractions and therefore may change behaviour with upgrades of the platform.

Ref: Java Reflection javarevisited.blogspot.in

@SantiBailors 2017-12-20 11:27:33

I would add to the drawbacks "It breaks refactoring". For me that's the main reason to avoid reflection as much as possible.

@Fernando Gabrieli 2018-12-01 21:28:04

So it allows us (for example), to inspect the classes we have (whether we have instances of them or not), correct? By this i mean, get their methods or constructors and use them to create new instances/invoke them. Why do we say "changing the program behavior" if the behavior is already there but with different code? Why is it called "reflection"? Thanks

@Ravindra babu 2016-02-13 12:33:45

From java documentation page

java.lang.reflect package provides classes and interfaces for obtaining reflective information about classes and objects. Reflection allows programmatic access to information about the fields, methods and constructors of loaded classes, and the use of reflected fields, methods, and constructors to operate on their underlying counterparts, within security restrictions.

AccessibleObject allows suppression of access checks if the necessary ReflectPermission is available.

Classes in this package, along with java.lang.Class accommodate applications such as debuggers, interpreters, object inspectors, class browsers, and services such as Object Serialization and JavaBeans that need access to either the public members of a target object (based on its runtime class) or the members declared by a given class

It includes following functionality.

  1. Obtaining Class objects,
  2. Examining properties of a class (fields, methods, constructors),
  3. Setting and getting field values,
  4. Invoking methods,
  5. Creating new instances of objects.

Have a look at this documentation link for the methods exposed by Class class.

From this article (by Dennis Sosnoski, President, Sosnoski Software Solutions, Inc) and this article (security-explorations pdf):

I can see considerable drawbacks than uses of using Reflection

User of Reflection:

  1. It provides very versatile way of dynamically linking program components
  2. It is useful for creating libraries that work with objects in very general ways

Drawbacks of Reflection:

  1. Reflection is much slower than direct code when used for field and method access.
  2. It can obscure what's actually going on inside your code
  3. It bypasses the source code can create maintenance problems
  4. Reflection code is also more complex than the corresponding direct code
  5. It allows violation of key Java security constraints such as data access protection and type safety

General abuses:

  1. Loading of restricted classes,
  2. Obtaining references to constructors, methods or fields of a restricted class,
  3. Creation of new object instances, methods invocation, getting or setting field values of a restricted class.

Have a look at this SE question regarding abuse of reflection feature:

How do I read a private field in Java?

Summary:

Insecure use of its functions conducted from within a system code can also easily lead to the compromise of a Java security model. So use this feature sparingly

@Liedman 2008-09-01 08:52:29

Reflection is a language's ability to inspect and dynamically call classes, methods, attributes, etc. at runtime.

For example, all objects in Java have the method getClass(), which lets you determine the object's class even if you don't know it at compile time (e.g. if you declared it as an Object) - this might seem trivial, but such reflection is not possible in less dynamic languages such as C++. More advanced uses lets you list and call methods, constructors, etc.

Reflection is important since it lets you write programs that do not have to "know" everything at compile time, making them more dynamic, since they can be tied together at runtime. The code can be written against known interfaces, but the actual classes to be used can be instantiated using reflection from configuration files.

Lots of modern frameworks use reflection extensively for this very reason. Most other modern languages use reflection as well, and in scripting languages (such as Python) they are even more tightly integrated, since it feels more natural within the general programming model of those languages.

@Fernando Gabrieli 2018-12-01 21:40:04

So in other words, you can create an instance out of it's qualified name and the compiler won't complain about it (because say you use just a String for the class name). Then, at run time, if that class is not present you get an exception. You kind of bypassed the compiler in this case. Would you give me some specific use case for this? I just can't picture when i would choose it.

@Liedman 2019-01-15 12:25:34

@FernandoGabrieli while it is true that it is easy to create runtime errors with reflection, it is also perfectly possible to use reflection without risking runtime exceptions. As hinted in my answer, a common use of reflection is for libraries or frameworks, which explicitly can't know the structure of the application at compile time, since they are compiled separate from the application. Any library that uses "code by convention" is likely to use reflection, but not necessarily using magic strings.

@VdeX 2014-09-08 09:40:02

Java Reflection is quite powerful and can be very useful. Java Reflection makes it possible to inspect classes, interfaces, fields and methods at runtime, without knowing the names of the classes, methods etc. at compile time. It is also possible to instantiate new objects, invoke methods and get/set field values using reflection.

A quick Java Reflection example to show you what using reflection looks like:

Method[] methods = MyObject.class.getMethods();

    for(Method method : methods){
        System.out.println("method = " + method.getName());
    }

This example obtains the Class object from the class called MyObject. Using the class object the example gets a list of the methods in that class, iterates the methods and print out their names.

Exactly how all this works is explained here

Edit: After almost 1 year I am editing this answer as while reading about reflection I got few more uses of Reflection.

  • Spring uses bean configuration such as:


<bean id="someID" class="com.example.Foo">
    <property name="someField" value="someValue" />
</bean>

When the Spring context processes this < bean > element, it will use Class.forName(String) with the argument "com.example.Foo" to instantiate that Class.

It will then again use reflection to get the appropriate setter for the < property > element and set its value to the specified value.

  • Junit uses Reflection especially for testing Private/Protected methods.

For Private methods,

Method method = targetClass.getDeclaredMethod(methodName, argClasses);
method.setAccessible(true);
return method.invoke(targetObject, argObjects);

For private fields,

Field field = targetClass.getDeclaredField(fieldName);
field.setAccessible(true);
field.set(object, value);

@Ess Kay 2013-05-06 20:16:28

Reflection has many uses. The one I am more familiar with, is to be able to create code on the fly.

IE: dynamic classes, functions, constructors - based on any data (xml/array/sql results/hardcoded/etc..)

@ThisClark 2017-02-15 01:53:59

This answer would be a lot better if you gave just one unusual example of generated code either from a SQL result or XML file etc.

@Ess Kay 2017-02-16 18:41:11

No problem. I used reflection in a windows application that dynamically generates the interface based on XML taken from a database.

@Ess Kay 2017-02-16 18:45:10

So basically, there is a class I created which shows the user a report. This report has parameters such as Date(from to) id, or whatever else. This info is stored in xml. So first we have a report selection. Based on the report selected, the form gets the xml. Once the xml is retrieved, it uses reflection to create a class with fields based on reflected types. Once you change to a different report, the slate is wiped clean and new fields are generated based on the xml. So its essentially a dynamic form based on reflection. I also used in other ways but this should be suffecient Hope that helps

@nazar_art 2014-09-20 22:22:03

I just want to add some point to all that was listed.

With Reflection API you can write universal toString() method for any object.

It is useful at debugging.

Here is some example:

class ObjectAnalyzer {

   private ArrayList<Object> visited = new ArrayList<Object>();

   /**
    * Converts an object to a string representation that lists all fields.
    * @param obj an object
    * @return a string with the object's class name and all field names and
    * values
    */
   public String toString(Object obj) {
      if (obj == null) return "null";
      if (visited.contains(obj)) return "...";
      visited.add(obj);
      Class cl = obj.getClass();
      if (cl == String.class) return (String) obj;
      if (cl.isArray()) {
         String r = cl.getComponentType() + "[]{";
         for (int i = 0; i < Array.getLength(obj); i++) {
            if (i > 0) r += ",";
            Object val = Array.get(obj, i);
            if (cl.getComponentType().isPrimitive()) r += val;
            else r += toString(val);
         }
         return r + "}";
      }

      String r = cl.getName();
      // inspect the fields of this class and all superclasses
      do {
         r += "[";
         Field[] fields = cl.getDeclaredFields();
         AccessibleObject.setAccessible(fields, true);
         // get the names and values of all fields
         for (Field f : fields) {
            if (!Modifier.isStatic(f.getModifiers())) {
               if (!r.endsWith("[")) r += ",";
               r += f.getName() + "=";
               try {
                  Class t = f.getType();
                  Object val = f.get(obj);
                  if (t.isPrimitive()) r += val;
                  else r += toString(val);
               } catch (Exception e) {
                  e.printStackTrace();
               }
            }
         }
         r += "]";
         cl = cl.getSuperclass();
      } while (cl != null);

      return r;
   }    
}

@Nikhil Shekhar 2013-07-08 16:12:04

Reflection allows instantiation of new objects, invocation of methods, and get/set operations on class variables dynamically at run time without having prior knowledge of its implementation.

Class myObjectClass = MyObject.class;
Method[] method = myObjectClass.getMethods();

//Here the method takes a string parameter if there is no param, put null.
Method method = aClass.getMethod("method_name", String.class); 

Object returnValue = method.invoke(null, "parameter-value1");

In above example the null parameter is the object you want to invoke the method on. If the method is static you supply null. If the method is not static, then while invoking you need to supply a valid MyObject instance instead of null.

Reflection also allows you to access private member/methods of a class:

public class A{

  private String str= null;

  public A(String str) {
  this.str= str;
  }
}

.

A obj= new A("Some value");

Field privateStringField = A.class.getDeclaredField("privateString");

//Turn off access check for this field
privateStringField.setAccessible(true);

String fieldValue = (String) privateStringField.get(obj);
System.out.println("fieldValue = " + fieldValue);
  • For inspection of classes (also know as introspection) you don't need to import the reflection package (java.lang.reflect). Class metadata can be accessed through java.lang.Class.

Reflection is a very powerful API but it may slow down the application if used in excess, as it resolves all the types at runtime.

@pramod 2012-02-06 05:37:25

As per my understanding:

Reflection allows programmer to access entities in program dynamically. i.e. while coding an application if programmer is unaware about a class or its methods, he can make use of such class dynamically (at run time) by using reflection.

It is frequently used in scenarios where a class name changes frequently. If such a situation arises, then it is complicated for the programmer to rewrite the application and change the name of the class again and again.

Instead, by using reflection, there is need to worry about a possibly changing class name.

@toolkit 2008-09-01 09:30:55

Reflection is a key mechanism to allow an application or framework to work with code that might not have even been written yet!

Take for example your typical web.xml file. This will contain a list of servlet elements, which contain nested servlet-class elements. The servlet container will process the web.xml file, and create new a new instance of each servlet class through reflection.

Another example would be the Java API for XML Parsing (JAXP). Where an XML parser provider is 'plugged-in' via well-known system properties, which are used to construct new instances through reflection.

And finally, the most comprehensive example is Spring which uses reflection to create its beans, and for its heavy use of proxies

@Mendelt 2008-09-01 08:50:57

Not every language supports reflection but the principles are usually the same in languages that support it.

Reflection is the ability to "reflect" on the structure of your program. Or more concrete. To look at the objects and classes you have and programmatically get back information on the methods, fields, and interfaces they implement. You can also look at things like annotations.

It's usefull in a lot of situations. Everywhere you want to be able to dynamically plug in classes into your code. Lot's of object relational mappers use reflection to be able to instantiate objects from databases without knowing in advance what objects they're going to use. Plug-in architectures is another place where reflection is usefull. Being able to dynamically load code and determine if there are types there that implement the right interface to use as a plugin is important in those situations.

@Atom 2015-06-22 18:45:41

I need to instantiate objects based on the data present in DB. I believe this is what you are saying about. Sample code would help me a lot. Thanks in advance.

@Jorge Córdoba 2008-09-01 08:50:52

Reflection is a set of functions which allows you to access the runtime information of your program and modify it behavior (with some limitations).

It's useful because it allows you to change the runtime behaivour depending on the meta information of your program, that is, you can check the return type of a function and change the way you handle the situation.

In C# for example you can load an assembly (a .dll) in runtime an examine it, navigating through the classes and taking actions according to what you found. It also let you create an instance of a class on runtime, invoke its method, etc.

Where can it be useful? Is not useful everytime but for concrete situations. For example you can use it to get the name of the class for loggin purposes, to dinamically create handlers for events according to what's specified on a configuration file and so on...

Related Questions

Sponsored Content

23 Answered Questions

42 Answered Questions

[SOLVED] "implements Runnable" vs "extends Thread" in Java

34 Answered Questions

[SOLVED] What is dependency injection?

7 Answered Questions

[SOLVED] What is the difference between canonical name, simple name and class name in Java Class?

  • 2013-03-04 13:45:28
  • Mohamed Taher Alrefaie
  • 214504 View
  • 883 Score
  • 7 Answer
  • Tags:   java

9 Answered Questions

[SOLVED] What is JSONP, and why was it created?

9 Answered Questions

[SOLVED] Why is subtracting these two times (in 1927) giving a strange result?

  • 2011-07-27 08:15:58
  • Freewind
  • 611339 View
  • 6508 Score
  • 9 Answer
  • Tags:   java date timezone

15 Answered Questions

[SOLVED] What is a JavaBean exactly?

18 Answered Questions

[SOLVED] Why is char[] preferred over String for passwords?

24 Answered Questions

[SOLVED] What is a serialVersionUID and why should I use it?

30 Answered Questions

[SOLVED] How to avoid Java code in JSP files?

  • 2010-07-05 07:24:06
  • chmoelders
  • 272183 View
  • 1625 Score
  • 30 Answer
  • Tags:   java jsp scriptlet

Sponsored Content