Reflection in Java
The reflection library is the main tool, that write programs which manipulate java code dynamically. There are programs which we can use to find out the capabilities of classes which known as reflective.
Java provide the features of inspecting classes, interfaces, fields and methods at run time even without knowing their names. It is also possible to instantiate new objects, invoke methods and get/set field values using reflection. This occur at run time irrespective of the access specifier used with them.
To get main information about,
- Class — getClass() — Class which the object belongs.
- Methods — getMethods() — Get the public methods of the class to which an objects belongs.
- Constructors — getConstructors() — Get the public constructors of the class to which an object belongs.
Other than these main methods there are bunch of other methods can be used to perform several operations.
getSuperclass(),getClasses(),getDeclaredClasses(),getDeclaringClass(),getPackage(),getModifiers(),getTypeParameters(),getFields() are some of those methods which we use in reflection.
Java run time system always maintain the run time type identification on all objects and it will useful to identify for which class the object belongs. The usage of this method is to select the correct methods to execute by virtual machine. To access these information we can use special java class called Class .
Get Shamild’s stories in your inbox
Join Medium for free to get updates from this writer.
Particular type of object describes the properties of particular class. More generally if we define object variable as “Class c1 = e.getClass();”, then whatever type of object (e) gives its class properties. To get the class name mostly using .getName() method.
If we know the class name and we need to obtain a class we can use static forName() method.
To create new instant in another way. “e.getClass().newInstance();” is call for the particular class.
Now let us see how can analyze the capabilities of the class using Reflection.
Within the java.lang.reflect package there are mainly three classes called Field, Method and Constructor. Those classes provide properties of the respective type within the given class. All three classes have a method called getName that returns the name of the item. The Field class has a method getType that returns an object, again of type Class, that describes the field type. All three of these classes also have a method called getModifiers that returns an integer, with various bits turned on and off, that describes the modifiers used, such as public and static. Use methods like isPublic, isPrivate, or isFinal in the Modifier class to tell whether a method or constructor was public, private, or final.
Finally we can use this concept to find out values assigned for variables which user cannot see. (Example code is below)
Here, once getVariableValues() method calls it gets object name and the variable name as the string in arguments. Then the program checks particular class and the variable name for given object. Finally returns the value of given variable.









