The Java Reflection API provides insight into the classes, interfaces, and objects in the JVM. Developers often use APIs to accomplish the following tasks, which explains why they always use development tools, such as debugger and integrated Development environments (IDEs):
· Determines the class of an object.
· Get information about a class of modifiers, fields, methods, constructors, etc.
· Gets the constants and method declarations about an interface.
· Creates a instance of a class whose name is not known until run time, and is available at design time or as a running parameter.
· Gets and sets the field value of an object, even if the name of the field is not known until the Run-time program.
· Calls a method on an object, even if the method is not known until run time.
The specific use of reflection is in JavaBeans, where you can manipulate software components with a build tool. Tools use reflection to get properties of Java components (classes) because they are loaded dynamically.
Use reflection to retrieve class Behavior
To understand how you use reflection to determine a class behavior, consider a simple example of the following employee class:
public class Employee
{public
String empnum;
Public String EmpName;
Public Employee ()
{This
("1", "King");
Public
Employee (string empnum, String empname)
{
empnum = empnum;
EmpName = EmpName;
Public
String toString ()
{return
"Employee details:empnumber:" + Empnum + ",
empname:" + Empnum;
}
}
import java.lang.reflect.Modifier;
public class Analyzeclass
{public
static void Main (string[] args)
{
Employee employee = new E Mployee ();
Class Klass = Employee.getclass ();
System.out.println ("Class name:" + klass.getname ());
System.out.println (
"Class Super Class:" + Klass.getsuperclass ());
int mods = Klass.getmodifiers ();
System.out.println (
"Class is public:" + modifier.ispublic (mods));
System.out.println (
"Class is final:" + modifier.isfinal (mods));
System.out.println (
"Class is Abstract:" + modifier.isabstract (mods));
}