Tutorial by Examples

Basics The Reflection API allows one to check the class structure of the code at runtime and invoke code dynamically. This is very powerful, but it is also dangerous since the compiler is not able to statically determine whether dynamic invocations are valid. A simple example would be to get the p...
Using reflection, a method of an object can be invoked during runtime. The example shows how to invoke the methods of a String object. import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; String s = "Hello World!"; // method without parameters // i...
Using the Reflection API, it is possible to change or get the value of a field at runtime. For example, you could use it in an API to retrieve different fields based on a factor, like the OS. You can also remove modifiers like final to allow modifing fields that are final. To do so, you will need t...
Getting the Constructor Object You can obtain Constructor class from the Class object like this: Class myClass = ... // get a class object Constructor[] constructors = myClass.getConstructors(); Where the constructors variable will have one Constructor instance for each public constructor decl...
Giving this enumeration as Example: enum Compass { NORTH(0), EAST(90), SOUTH(180), WEST(270); private int degree; Compass(int deg){ degree = deg; } public int getDegree(){ return degree; } } In Java an enum class is like any other c...
Given a String containing the name of a class, it's Class object can be accessed using Class.forName: Class clazz = null; try { clazz = Class.forName("java.lang.Integer"); } catch (ClassNotFoundException ex) { throw new IllegalStateException(ex); } Java SE 1.2 It can be s...
Example: Invoke different constructors by passing relevant parameters import java.lang.reflect.*; class NewInstanceWithReflection{ public NewInstanceWithReflection(){ System.out.println("Default constructor"); } public NewInstanceWithReflection( String a){ ...
Reflection is useful when it is properly used for right purpose. By using reflection, you can access private variables and re-initialize final variables. Below is the code snippet, which is not recommended. import java.lang.reflect.*; public class ReflectionDemo{ public static void main(St...
If you want to create an instance of an inner nested class you need to provide a class object of the enclosing class as an extra parameter with Class#getDeclaredConstructor. public class Enclosing{ public class Nested{ public Nested(String a){ System.out.println("Constru...
Dynamic Proxies do not really have much to do with Reflection but they are part of the API. It's basically a way to create a dynamic implementation of an interface. This could be helpful when creating mockup services. A Dynamic Proxy is an instance of an interface that is created with a so-called in...
The Reflection API could be used to change values of private and final fields even in the JDK default library. This could be used to manipulate the behaviour of some well known classes as we will see. What is not possible Lets start first with the only limitation means the only field we can't chan...

Page 1 of 1