- 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.
http://docs.oracle.com/javase/tutorial/reflect/index.html
- Reflection allows the examination/modification of classes/objects at runtime. Personally I have only used it to implement a factory pattern. This is where there are a number of classes/subclasses that could be used to create objects at runtime. Based on some variable I can create the correct object by loading the class I want.
Class<?> c = Class.forName(someclass);
superTypeOfClass = c.newInstance();
I believe the instanceOf (isInstance() maybe) is also part of the Reflection API allowing you to discover the run time type of an object. Hope this helps.
Another example is Spring core framework's depedency injection. It is done through Reflection API.
Reflection API helps to manipulate classes and its properties at run time. You can create instance for class and can execute all methods (including private).
For example, To test private methods in JUnit.
http://www.linkedin.com/groups/What-is-Reflection-API-Can-70526.S.225700742?view=&srchtype=discussedNews&gid=70526&item=225700742&type=member&trk=eml-anet_dig-b_pd-ttl-cn&ut=0j-X2_38EiZRE1
No comments:
Post a Comment