Java Reflection API (java.lang.Class)
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
Where it is used
-
IDE (Integrated Development Environment) e.g. Eclipse, MyEclipse, NetBeans etc.
-
Debugger
-
Test Tools etc
1. Java.lang.Class
The java.lang.Class class performs mainly two tasks:
-
Provides methods to get the metadata of a class at run time.
-
Provides methods to examine and change the run time behavior of a class.
Method | Description |
---|---|
public String getName() | returns the class name |
public static Class forName(String className)throws ClassNotFoundException | Loads the class and returns the reference of Class class. |
public Object newInstance()throws InstantiationException,IllegalAccessException | Creates new instance. |
public boolean isInterface() | Checks if it is interface. |
public boolean isArray() | Checks if it is array. |
public boolean isPrimitive() | Checks if it is primitive. |
public Class getSuperclass() | Returns the superclass class reference. |
public Field[] getDeclaredFields() | Returns the total number of fields of this class. |
public Method[] getDeclaredMethods() | Returns the total number of methods of this class. |
public Constructor[] getDeclaredConstructors() | Returns the total number of constructors of this class. |
public Method getDeclaredMethod(String name,Class[] parameterTypes)throws NoSuchMethodException,SecurityException | Returns the method class instance. |
PREVIOUSGarbage Collection