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 {
public static void main(String[] args) {
System.out.println(“Testing classloading…”);
Class clazz = Class.forName(“com.sun1.TestClass”);
Method [] methods = clazz.getDeclaredMethods();
TestClass tc = new TestClass();
for (Method method : methods) {
method.invoke(tc, new Object[]{“Sunil Chauraha”});
}catch(InvocationTargetException e){
}catch (IllegalAccessException e) {
}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);

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: