This java code example shows how to invoke methods on an object at runtime without knowing the names of them in advance.
This is possible using the reflection API. What we do is to get an instance of the class object of the particular class we want to call methods on and by using that instance we can get the array of Method objects by calling getDeclaredMethods().
We dont actually call the methods of the class instance, instead we have to create an object that we call the methods on by sending it as an argument to the invoke() method.
In this example we use an inner class named "Computer" but of course it could be any object, not necessarily an instance of an inner class. The names of the methods invoked are printed out along with the values that they return.

Code Examples For Java Reflection API

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 *
 * @author javadb.com
 */

public class Main {
    
    /**
     * Invokes the methods of an object using the Reflection api.
     */

    public void invokeMethodsUsingReflection() {

        //Obtain the Class instance
        Class computerClass = Computer.class;
        
        //Get the methods
        Method[] methods = computerClass.getDeclaredMethods();
        
        //Create the object that we want to invoke the methods on
        Computer computer = new Computer();
        
        //Loop through the methods and invoke them
        for (Method method : methods) {
            Object result;
            try {
                //Call the method. Since none of them takes arguments we just
                //pass an empty array as second parameter.
                result = method.invoke(computer, new Object[0]);
            } catch (IllegalArgumentException ex) {
                ex.printStackTrace();
                return;
            } catch (InvocationTargetException ex) {
                ex.printStackTrace();
                return;
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
                return;
            }
            System.out.println(method.getName() + ": " + result);
        }

/**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        new Main().invokeMethodsUsingReflection();
    }
    
    
    class Computer {
        
        private String brand = "DELL";
        private String type = "Laptop";
        private int harddiskSize_GB = 300;
        private boolean AntiVirusInstalled = true;

        public String getBrand() {
            return brand;
        }

        public String getType() {
            return type;
        }

        public int getHarddiskSize_GB() {
            return harddiskSize_GB;
        }

        public boolean isAntiVirusInstalled() {
            return AntiVirusInstalled;
        }
    }
}




Sandeep Joshi
Mathematics, Technology and Programming are my passion. I am a part of Java Ecosystem and through this blog, I contribute to it. I am here to blog about my interests, views and experiences.
I am on Google+ and Facebook.
I feel proud to be listed as a "National Memory Record Holder" in the Limca Book of Records, 2009 and have attempted for an International Memory record in the Guiness Book of Records. I can remember the value of PI upto 10,000 digits after the decimal (3.1415.....). You can contact me on javagenious.com(At)gmal.com ; I would like to hear from you :)