Class loading and java reflection
Here is some simple example of class loading and java reflection.
package com.sun;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import com.sun1.TestClass;
public class MainClass {
@SuppressWarnings(“unchecked”)
public static void main(String[] args) {
System.out.println(“Testing classloading…”);
try{
Class clazz = Class.forName(“com.sun1.TestClass”);
Method [] methods = clazz.getDeclaredMethods();
TestClass tc = new TestClass();
for (Method method : methods) {
try{
method.invoke(tc, new Object[]{“Sunil Chauraha”});
}catch(InvocationTargetException e){
System.err.println(e.getMessage());
}catch (IllegalAccessException e) {
System.err.println(e.getMessage());
}
}
}catch(ClassNotFoundException cnf){
System.err.println(cnf.getMessage()+” : “+cnf);
}
}
}
package com.sun1;
public class TestClass {
public void pring(String value){
System.out.println(“The value passed is : “+value);
}
}
No trackbacks yet.