Header Ads

  • Breaking Now

    Code Snippets: Using java.lang.reflect.* APIs

    import java.lang.reflect.*;
    public class ReflectionExample {
        public static void main(String args[])
                ClassNotFoundException, InstantiationException,
                IllegalAccessException, NoSuchMethodException, 
            ClassLoader j = ClassLoader.getSystemClassLoader();
            Class someClass = j.loadClass("GlobalVillage");
            Object instanceOfSomething =  someClass.newInstance();
            // the second parameter specifies the type of the argument(s) passed to method
            Method aMethod = someClass.getMethod("showMessage", new Class[]{String.class});
            //Here value of the argument of the method is provided
            Object returnValue = aMethod.invoke(instanceOfSomething, "The whole world is like a family.");
            System.out.println("Show message says : " + returnValue); 
    class GlobalVillage {
        public String showMessage(String strMessage) {
            return strMessage; // isn't this true??

    Post Top Ad

    Post Bottom Ad