The document discusses the Reflection API in Java. It provides 3 key points:
1. Reflection API allows programs to inspect and modify their own structure and behavior at runtime. This includes getting information about classes, interfaces, fields and methods.
2. Common Reflection API methods include getClass(), Class.forName(), getDeclaredMethods(), and getDeclaredMethod() which allow accessing details about classes and methods.
3. newInstance() allows creating an object of a class without using the new operator. It instead uses reflection to create the instance.
The document discusses the Reflection API in Java. It provides 3 key points:
1. Reflection API allows programs to inspect and modify their own structure and behavior at runtime. This includes getting information about classes, interfaces, fields and methods.
2. Common Reflection API methods include getClass(), Class.forName(), getDeclaredMethods(), and getDeclaredMethod() which allow accessing details about classes and methods.
3. newInstance() allows creating an object of a class without using the new operator. It instead uses reflection to create the instance.
Reflection API is a powerful technique to find out the environment of the class as well as to inspect the class itself.
Reflection API is introduced in Java 1.1.
The classes of Reflection API are the part of the package java.lang.reflect and methods are the part of java.lang.class.
It allows the user to get complete information about classes,interfaces,constructors,fields and various methods being used.
It also provides an easy way to develop java applications which is not possible before java 1.1.
We can create the methods like event handler, hash code etc and also we can find out objects and classes.
Avoid using of the reflection API wherever it affects the performance of an application like applet applications.
We are using reflection API to mapping objects into tables in database at runtime.
2
KEERTHANA TECHNOLOGIES 8050673080 Reflection API Methods getClass(), classname.class, Class.forName(classname with package); These methods are used to get information about the classes which are declared. Here whenever class is loaded into stack memory java.lang.class object is created for respective class. package ReflectionAPI; class H {
} class H2 extends H{
} public class H1 { public static void main(String[] args) { H h = new H(); H1 h1 = new H1(); H2 h2 = new H2(); System.out.println("reference variable h"); Class c1 = h.getClass(); Class c2 = h1.getClass(); Class c3 = h2.getClass();
} public class H1 { public static void main(String[] args) throws Exception{ H h = new H(); Class c4 = Class.forName("ReflectionAPI.H"); Class c5 = H.class; Class c6 = h.getClass();
KEERTHANA TECHNOLOGIES 8050673080 newInstance() This method is used to create an object without using new keyword to access the instance members of the respective class. package ReflectionAPI;
public class NewInstance { public static void main(String[] args) throws Exception{
NewInstance n1 = new NewInstance(); Class c1 = n1.getClass(); NewInstance n2 = (NewInstance)c1.newInstance(); System.out.println(n1); System.out.println(c1); System.out.println(n2); System.out.println(n1 == n2);
}} Output: ReflectionAPI.NewInstance@1034bb5 class ReflectionAPI.NewInstance ReflectionAPI.NewInstance@15f5897 false
package ReflectionAPI;
public class NewInstance1 { NewInstance1(){ System.out.println("Constructor"); }
NewInstance1(String s ,int y){ System.out.println("Constructor(String s .int y)"); }
public static void main(String[] args) throws Exception{ NewInstance1 n1 = new NewInstance1(); NewInstance1 n11 = new NewInstance1(12); NewInstance1 n12 = new NewInstance1("keerthana",67);
Class c1 = n1.getClass(); Class c2 = n11.getClass(); NewInstance1 n2 = (NewInstance1)c1.newInstance();
}} Output: Constructor Constructor(int x) Constructor(String s .int y) Constructor
6
KEERTHANA TECHNOLOGIES 8050673080 getDeclaredMethod(method name) getDeclaredMethods() getDeclaredMethod() method is used to get the name of the method of a particular class. It is in Class class and accepts method name as an argument. The return type of this method is Method object type or reference type. getDeclaredMethods() is used to get the methods using array with for loop to get the method name.
public static void main(String[] args) throws Exception{
Scanner sc = new Scanner(System.in); System.out.println("enter the class name"); String className = sc.next(); System.out.println("enter the method name"); String methodName = sc.next(); Class c1 = Class.forName("ReflectionAPI " + "." + className); // Object obj =c1.newInstance(); Method m1[] = c1.getDeclaredMethods(); for(Method m : m1){ System.out.println(m);
}
}} Output: enter the class name M14 enter the method name metod1 from method1
9
KEERTHANA TECHNOLOGIES 8050673080 getParameterTypes() : This method is used to get the parameters passed in the methods which is declared in the class. getModifiers() :This method is used to get the access specifiers of a class and its return type is int.
package ReflectionAPI;
import java.lang.reflect.Method;
public class Parameter { void method1(int x,int y){ System.out.println("from method1 " + x + "," + y );
} void method2(String s , double d){ System.out.println("from method2 " + s + "," +d) ;
KEERTHANA TECHNOLOGIES 8050673080 getDeclaredConstructors() : This method is used to get the constructors declared in the class with class name. getPackage() : From this method we can get the package name in which the class is declared. Its return type is char[] and return type is null. Syntax public static char[] getPackage() { return null; }
package ReflectionAPI;
import java.lang.reflect.Constructor;
class Cons1{ Cons1(){ System.out.println("default Constructor"); } Cons1(int x){
public class Client1 { public static void main(String[] args) { A obj1 = new A(); System.out.println(obj1); System.out.println(obj1.toString()); Class c2 = Client1.class; }
public static char[] getPackage() { return null; }
}
package ReflectionAPI; import toString.Client1;
public class Pack1 { public static void main(String[] args) throws Exception{
Class c1 = Pack1.class; Client1 c2 = new Client1(); System.out.println(c1.getPackage()); System.out.println( Client1.getPackage());//CTE System.out.println(c2.getPackage());//CTE }
} Output : package ReflectionAPI Exception in thread "main" java.lang.NullPointerException
12
KEERTHANA TECHNOLOGIES 8050673080 getDeclaredFields(), getName(), getType() from these methods we can get the type ,name and reference of the field s those are declared in the class.
package ReflectionAPI;
import java.lang.reflect.Field;
public class FieldsFd { static int i; final String s = "Keerthana"; private byte b; protected float f; public static void main(String[] args) {
Class c1 = FieldsFd .class; Field fd[] = c1.getDeclaredFields(); for(Field f : fd){ System.out.println("References : " +f); System.out.println("Field Name :" +f.getName()); System.out.println("Field type : " +f.getType()); System.out.println("----------------------"); }
}}
Output: References : static int ReflectionAPI.FieldsFd.i Field Name :i Field type : int ---------------------- References : final java.lang.String ReflectionAPI.FieldsFd.s Field Name :s Field type : class java.lang.String ---------------------- References : private byte ReflectionAPI.FieldsFd.b Field Name :b Field type : byte ---------------------- References : protected float ReflectionAPI.FieldsFd.f Field Name :f Field type : float ----------------------