714

If I have two variables:

Object obj;
String methodName = "getName";

Without knowing the class of obj, how can I call the method identified by methodName on it?

The method being called has no parameters, and a String return value. It's a getter for a Java bean.

Joachim Sauer
  • 278,207
  • 54
  • 523
  • 586
brasskazoo
  • 68,343
  • 22
  • 59
  • 74
  • 4
    Either use [the reflection api](http://java.sun.com/docs/books/tutorial/reflect/index.html) or use [groovy](http://groovy.codehaus.org/GPath) – Peter Kelley Oct 02 '08 at 05:21

22 Answers22

1017

Coding from the hip, it would be something like:

java.lang.reflect.Method method;
try {
  method = obj.getClass().getMethod(methodName, param1.class, param2.class, ..);
} catch (SecurityException e) { ... }
  catch (NoSuchMethodException e) { ... }

The parameters identify the very specific method you need (if there are several overloaded available, if the method has no arguments, only give methodName).

Then you invoke that method by calling

try {
  method.invoke(obj, arg1, arg2,...);
} catch (IllegalArgumentException e) { ... }
  catch (IllegalAccessException e) { ... }
  catch (InvocationTargetException e) { ... }

Again, leave out the arguments in .invoke, if you don't have any. But yeah. Read about Java Reflection

Andrew Tobilko
  • 44,067
  • 12
  • 74
  • 128
Henrik Paul
  • 63,711
  • 30
  • 82
  • 93
  • 3
    Was a little upset by the fact that Java uses type erasure, but knowing that at least it has Reflection cheers me up again :D And now with lambdas in Java 8 the language is really getting up to speed with modern development. Only thing missing now is native support to getters and setters, or properties as they're known in C#. – 7hi4g0 Apr 01 '14 at 13:23
  • 126
    Not a fair -1. Henrik is probably not advocating squashing exceptions and didn't write anything for them because he is just trying to demonstrate reflection. – drew May 21 '14 at 20:52
  • 74
    Plus one for showing some potential exceptions. If I had written this, it would be ... catch(Exception e){ ... – mikbanUtah Dec 11 '14 at 06:32
  • 1
    I got "variable may not have been initialized" for the `method` in `method.invoke(obj, arg1, arg2,...);`. a `method = null;` solves the problem but mentioning it in the answer is not a bad idea. – Amin Feb 14 '16 at 18:14
  • Am I missing something or does this invocation not return a java error code. For example if the method ends with anything other than System.exit(0), this technique will not report it? Or does an exception exist for this? – DeaMon1 Jul 19 '16 at 19:13
  • 2
    @DeaMon1 Java methods don't use "exit codes", but if the method returns anything, `invoke` will return whatever it returned. If an exception occurs running the method, the exception will be wrapped in an `InvocationTargetException`. – ThePyroEagle Dec 22 '16 at 15:28
  • @ThePyroEagle eh yeah, I meant return code from the class post execution. – DeaMon1 Jan 16 '17 at 19:29
  • 1
    Why would you **not** use catch (Exception e) for something like this? – Andrew Jun 20 '17 at 16:49
  • 1
    @7hi4g0 If you just wanted getters and setters, there's https://projectlombok.org/ which also gives you things like annotations to create automatic loggers, val and var "types", automatic equals and hashcodes, toString creation, etc. All without filling your code up with autogenerated code that nobody wants to see. It's pretty awesome. If only Java could formalize support for compile time macros, and it would be ahead of every non-lisp language. – Haakon Løtveit Aug 10 '17 at 08:21
  • @Andrew you wouldn't want to catch only Exception if you want to know exactly what was the problem and deal with each exception subtype accordingly – RJFares Nov 29 '17 at 07:14
  • Especially with `InvocationTargetException` where your catch block would probably `throw e.getCause();` to get the Exception thrown by the target method itself. – neXus Sep 10 '18 at 12:54
208

Use method invocation from reflection:

Class<?> c = Class.forName("class name");
Method method = c.getDeclaredMethod("method name", parameterTypes);
method.invoke(objectToInvokeOn, params);

Where:

  • "class name" is the name of the class
  • objectToInvokeOn is of type Object and is the object you want to invoke the method on
  • "method name" is the name of the method you want to call
  • parameterTypes is of type Class[] and declares the parameters the method takes
  • params is of type Object[] and declares the parameters to be passed to the method
Neuron
  • 3,776
  • 3
  • 24
  • 44
Owen
  • 2,541
  • 1
  • 13
  • 9
  • Cool, I think you're right with getDeclaredMethod(), it is probably 'safer' than getMethod().. – brasskazoo Oct 02 '08 at 05:40
  • 23
    Wrong. Yes, getDeclaredMethod does work with private and protected methods. BUT: it does not work with methods defined in superclasses (inherited methods). So, it depends strongly on what you want to do. In many cases you want it to work regardless of the exact class in which the method is defined. – jrudolph Oct 02 '08 at 09:12
  • And where should I put "class" file? preferably explain for Eclipse IDE – Dr.jacky Aug 08 '16 at 11:04
  • @Mr.Hyde on the class path. – Stijn de Witt Mar 09 '17 at 11:31
  • What should I put inside of and method.invoke() if the method I'm calling doesn't accept any parameters at all? It seems that I still have to provide second parameter, should it be some empty Object array? – Igor Apr 23 '20 at 15:07
113

For those who want a straight-forward code example in Java 7:

Dog class:

package com.mypackage.bean;

public class Dog {
    private String name;
    private int age;

    public Dog() {
        // empty constructor
    }

    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void printDog(String name, int age) {
        System.out.println(name + " is " + age + " year(s) old.");
    }
}

ReflectionDemo class:

package com.mypackage.demo;

import java.lang.reflect.*;

public class ReflectionDemo {

    public static void main(String[] args) throws Exception {
        String dogClassName = "com.mypackage.bean.Dog";
        Class<?> dogClass = Class.forName(dogClassName); // convert string classname to class
        Object dog = dogClass.newInstance(); // invoke empty constructor

        String methodName = "";

        // with single parameter, return void
        methodName = "setName";
        Method setNameMethod = dog.getClass().getMethod(methodName, String.class);
        setNameMethod.invoke(dog, "Mishka"); // pass arg

        // without parameters, return string
        methodName = "getName";
        Method getNameMethod = dog.getClass().getMethod(methodName);
        String name = (String) getNameMethod.invoke(dog); // explicit cast

        // with multiple parameters
        methodName = "printDog";
        Class<?>[] paramTypes = {String.class, int.class};
        Method printDogMethod = dog.getClass().getMethod(methodName, paramTypes);
        printDogMethod.invoke(dog, name, 3); // pass args
    }
}

Output: Mishka is 3 year(s) old.


You can invoke the constructor with parameters this way:

Constructor<?> dogConstructor = dogClass.getConstructor(String.class, int.class);
Object dog = dogConstructor.newInstance("Hachiko", 10);

Alternatively, you can remove

String dogClassName = "com.mypackage.bean.Dog";
Class<?> dogClass = Class.forName(dogClassName);
Object dog = dogClass.newInstance();

and do

Dog dog = new Dog();

Method method = Dog.class.getMethod(methodName, ...);
method.invoke(dog, ...);

Suggested reading: Creating New Class Instances

silver
  • 4,573
  • 12
  • 52
  • 82
56

The method can be invoked like this. There are also more possibilities (check the reflection api), but this is the simplest one:

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

import org.junit.Assert;
import org.junit.Test;

public class ReflectionTest {

    private String methodName = "length";
    private String valueObject = "Some object";

    @Test
    public void testGetMethod() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
            IllegalAccessException, InvocationTargetException {
        Method m = valueObject.getClass().getMethod(methodName, new Class[] {});
        Object ret = m.invoke(valueObject, new Object[] {});
        Assert.assertEquals(11, ret);
    }



}
Petr Macek
  • 1,503
  • 1
  • 15
  • 19
  • 8
    +1 for the only answer that recognized that the OP specified "no parameters" in his question (and because it was what I was looking for too). – John Fitzpatrick Jan 17 '13 at 17:54
16

First, don't. Avoid this sort of code. It tends to be really bad code and insecure too (see section 6 of Secure Coding Guidelines for the Java Programming Language, version 2.0).

If you must do it, prefer java.beans to reflection. Beans wraps reflection allowing relatively safe and conventional access.

Tom Hawtin - tackline
  • 139,906
  • 30
  • 206
  • 293
  • 12
    I disagree. It's very easy to write such code to be secure and I have done so in multiple languages. For example, one could make a set of allowable methods, and only allow a method to be invoked if it's name is in the set. Even more secure (yet still bone-head simple) would be limiting each allowed method to a specific state, and not allowing the method to be invoked unless the thread/interface/user/whatever fits such criteria. – JSON Feb 09 '15 at 09:39
  • Never be so categoricall about such issues. Right now I'm creating a simple program to allow the user to define arbitrary tasks over arbitrary objects using web interfaces. I know it is, indeed, insecure, but proper testing is performed once the config is received, and it allows a non-programmer to easilly configure the tasks, and also gives programmes the ability to link custom classes to the generic code (thats the part I use reflection to, in order to allow them to configure which methods to use via web interface) without having to update the GUI. – DGoiko Aug 04 '19 at 19:56
14

To complete my colleague's answers, You might want to pay close attention to:

  • static or instance calls (in one case, you do not need an instance of the class, in the other, you might need to rely on an existing default constructor that may or may not be there)
  • public or non-public method call (for the latter,you need to call setAccessible on the method within an doPrivileged block, other findbugs won't be happy)
  • encapsulating into one more manageable applicative exception if you want to throw back the numerous java system exceptions (hence the CCException in the code below)

Here is an old java1.4 code which takes into account those points:

/**
 * Allow for instance call, avoiding certain class circular dependencies. <br />
 * Calls even private method if java Security allows it.
 * @param aninstance instance on which method is invoked (if null, static call)
 * @param classname name of the class containing the method 
 * (can be null - ignored, actually - if instance if provided, must be provided if static call)
 * @param amethodname name of the method to invoke
 * @param parameterTypes array of Classes
 * @param parameters array of Object
 * @return resulting Object
 * @throws CCException if any problem
 */
public static Object reflectionCall(final Object aninstance, final String classname, final String amethodname, final Class[] parameterTypes, final Object[] parameters) throws CCException
{
    Object res;// = null;
    try {
        Class aclass;// = null;
        if(aninstance == null)
        {
            aclass = Class.forName(classname);
        }
        else
        {
            aclass = aninstance.getClass();
        }
        //Class[] parameterTypes = new Class[]{String[].class};
    final Method amethod = aclass.getDeclaredMethod(amethodname, parameterTypes);
        AccessController.doPrivileged(new PrivilegedAction() {
    public Object run() {
                amethod.setAccessible(true);
                return null; // nothing to return
            }
        });
        res = amethod.invoke(aninstance, parameters);
    } catch (final ClassNotFoundException e) {
        throw new CCException.Error(PROBLEM_TO_ACCESS+classname+CLASS, e);
    } catch (final SecurityException e) {
        throw new CCException.Error(PROBLEM_TO_ACCESS+classname+GenericConstants.HASH_DIESE+ amethodname + METHOD_SECURITY_ISSUE, e);
    } catch (final NoSuchMethodException e) {
        throw new CCException.Error(PROBLEM_TO_ACCESS+classname+GenericConstants.HASH_DIESE+ amethodname + METHOD_NOT_FOUND, e);
    } catch (final IllegalArgumentException e) {
        throw new CCException.Error(PROBLEM_TO_ACCESS+classname+GenericConstants.HASH_DIESE+ amethodname + METHOD_ILLEGAL_ARGUMENTS+String.valueOf(parameters)+GenericConstants.CLOSING_ROUND_BRACKET, e);
    } catch (final IllegalAccessException e) {
        throw new CCException.Error(PROBLEM_TO_ACCESS+classname+GenericConstants.HASH_DIESE+ amethodname + METHOD_ACCESS_RESTRICTION, e);
    } catch (final InvocationTargetException e) {
    throw new CCException.Error(PROBLEM_TO_ACCESS+classname+GenericConstants.HASH_DIESE+ amethodname + METHOD_INVOCATION_ISSUE, e);
    } 
    return res;
}
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
12
Object obj;

Method method = obj.getClass().getMethod("methodName", null);

method.invoke(obj, null);
chickeninabiscuit
  • 8,307
  • 10
  • 47
  • 55
  • Object should have at least value/values. – Lova Chittumuri Feb 12 '18 at 14:38
  • This worked really well for what I needed. I had a class that was already instantiated and just needed to get a method off of it. Adding catches for exceptions is a good idea here, but otherwise, this worked perfectly for me. I think my way of avoiding null exceptions was to use nullables, but I was using a very restricted range of method names (literally just a counter from 1 to 4). – Jain Waldrip Mar 25 '19 at 20:41
12
//Step1 - Using string funClass to convert to class
String funClass = "package.myclass";
Class c = Class.forName(funClass);

//Step2 - instantiate an object of the class abov
Object o = c.newInstance();
//Prepare array of the arguments that your function accepts, lets say only one string here
Class[] paramTypes = new Class[1];
paramTypes[0]=String.class;
String methodName = "mymethod";
//Instantiate an object of type method that returns you method name
 Method m = c.getDeclaredMethod(methodName, paramTypes);
//invoke method with actual params
m.invoke(o, "testparam");
anujin
  • 745
  • 7
  • 24
  • 36
11

Indexing (faster)

You can use FunctionalInterface to save methods in a container to index them. You can use array container to invoke them by numbers or hashmap to invoke them by strings. By this trick, you can index your methods to invoke them dynamically faster.

@FunctionalInterface
public interface Method {
    double execute(int number);
}

public class ShapeArea {
    private final static double PI = 3.14;

    private Method[] methods = {
        this::square,
        this::circle
    };

    private double square(int number) {
        return number * number;
    }

    private double circle(int number) {
        return PI * number * number;
    }

    public double run(int methodIndex, int number) {
        return methods[methodIndex].execute(number);
    }
}

Lambda syntax

You can also use lambda syntax:

public class ShapeArea {
    private final static double PI = 3.14;

    private Method[] methods = {
        number -> {
            return number * number;
        },
        number -> {
            return PI * number * number;
        },
    };

    public double run(int methodIndex, int number) {
        return methods[methodIndex].execute(number);
    }
}
Amir Fo
  • 3,044
  • 1
  • 25
  • 36
8

If you do the call several times you can use the new method handles introduced in Java 7. Here we go for your method returning a String:

Object obj = new Point( 100, 200 );
String methodName = "toString";  
Class<String> resultType = String.class;

MethodType mt = MethodType.methodType( resultType );
MethodHandle methodHandle = MethodHandles.lookup().findVirtual( obj.getClass(), methodName, mt );
String result = resultType.cast( methodHandle.invoke( obj ) );

System.out.println( result );  // java.awt.Point[x=100,y=200]
Christian Ullenboom
  • 1,118
  • 3
  • 22
  • 18
  • 1
    To future readers; If you care about performance you'll wanna use `invokeExact` whenever you can. For that, the call site signature has to match the method handle type exactly though. It usually takes a little tinkering to get to work. In this case you'd need to cast the first parameter with: `methodHandle = methodHandle.asType(methodHandle.type().changeParameterType(0, Object.class));` and then invoke like `String result = (String) methodHandle.invokeExact(obj);` – Jorn Vernee Feb 04 '18 at 12:09
7

This sounds like something that is doable with the Java Reflection package.

http://java.sun.com/developer/technicalArticles/ALT/Reflection/index.html

Particularly under Invoking Methods by Name:

import java.lang.reflect.*;

public class method2 {
  public int add(int a, int b)
  {
     return a + b;
  }

  public static void main(String args[])
  {
     try {
       Class cls = Class.forName("method2");
       Class partypes[] = new Class[2];
        partypes[0] = Integer.TYPE;
        partypes[1] = Integer.TYPE;
        Method meth = cls.getMethod(
          "add", partypes);
        method2 methobj = new method2();
        Object arglist[] = new Object[2];
        arglist[0] = new Integer(37);
        arglist[1] = new Integer(47);
        Object retobj 
          = meth.invoke(methobj, arglist);
        Integer retval = (Integer)retobj;
        System.out.println(retval.intValue());
     }
     catch (Throwable e) {
        System.err.println(e);
     }
  }
}
zxcv
  • 6,921
  • 8
  • 32
  • 30
6

Here are the READY TO USE METHODS:

To invoke a method, without Arguments:

public static void callMethodByName(Object object, String methodName) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    object.getClass().getDeclaredMethod(methodName).invoke(object);
}

To invoke a method, with Arguments:

    public static void callMethodByName(Object object, String methodName, int i, String s) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        object.getClass().getDeclaredMethod(methodName, int.class, String.class).invoke(object, i, s);
    }

Use the above methods as below:

package practice;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

public class MethodInvoke {

    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IOException {
        String methodName1 = "methodA";
        String methodName2 = "methodB";
        MethodInvoke object = new MethodInvoke();
        callMethodByName(object, methodName1);
        callMethodByName(object, methodName2, 1, "Test");
    }

    public static void callMethodByName(Object object, String methodName) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        object.getClass().getDeclaredMethod(methodName).invoke(object);
    }

    public static void callMethodByName(Object object, String methodName, int i, String s) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        object.getClass().getDeclaredMethod(methodName, int.class, String.class).invoke(object, i, s);
    }

    void methodA() {
        System.out.println("Method A");
    }

    void methodB(int i, String s) {
        System.out.println("Method B: "+"\n\tParam1 - "+i+"\n\tParam 2 - "+s);
    }
}

Output:

Method A  
Method B:  
	Param1 - 1  
	Param 2 - Test
Sandeep Nalla
  • 123
  • 1
  • 8
5

Please refer following code may help you.

public static Method method[];
public static MethodClass obj;
public static String testMethod="A";

public static void main(String args[]) 
{
    obj=new MethodClass();
    method=obj.getClass().getMethods();
    try
    {
        for(int i=0;i<method.length;i++)
        {
            String name=method[i].getName();
            if(name==testMethod)
            {   
                method[i].invoke(name,"Test Parameters of A");
            }
        }
    }
    catch(Exception ex)
    {
        System.out.println(ex.getMessage());
    }
}

Thanks....

Rahul Karankal
  • 93
  • 1
  • 12
  • This is not how you compare Strings in Java. You must use .equals method. Otherwise, you are just comparing that they are the same object reference, and you do not actually care about object references - just the string content being a match. You can also get the method by name via reflection, so not sure why you would roll your own? – Lo-Tan Dec 12 '19 at 19:41
5

I do this like this:

try {
    YourClass yourClass = new YourClass();
    Method method = YourClass.class.getMethod("yourMethodName", ParameterOfThisMethod.class);
    method.invoke(yourClass, parameter);
} catch (Exception e) {
    e.printStackTrace();
}
Neuron
  • 3,776
  • 3
  • 24
  • 44
Marcel
  • 2,175
  • 2
  • 19
  • 36
5
Method method = someVariable.class.getMethod(SomeClass);
String status = (String) method.invoke(method);

SomeClass is the class and someVariable is a variable.

Neuron
  • 3,776
  • 3
  • 24
  • 44
Subrahmanya Prasad
  • 566
  • 1
  • 8
  • 14
  • if someVariable is really an object, call someVariable.getClass(). Also, you cannot call getMethod() with a class as the only argument. Neither invoke method with method. Correct: someVariable.getClass().getMethod("coolMethod", parameterClasses).invoke(arguments); – lue Mar 01 '20 at 11:31
3

Student.java

class Student{
    int rollno;
    String name;

    void m1(int x,int y){
        System.out.println("add is" +(x+y));
    }

    private void m3(String name){
        this.name=name;
        System.out.println("danger yappa:"+name);
    }
    void m4(){
        System.out.println("This is m4");
    }
}

StudentTest.java

import java.lang.reflect.Method;
public class StudentTest{

     public static void main(String[] args){

        try{

            Class cls=Student.class;

            Student s=(Student)cls.newInstance();


            String x="kichha";
            Method mm3=cls.getDeclaredMethod("m3",String.class);
            mm3.setAccessible(true);
            mm3.invoke(s,x);

            Method mm1=cls.getDeclaredMethod("m1",int.class,int.class);
            mm1.invoke(s,10,20);

        }
        catch(Exception e){
            e.printStackTrace();
        }
     }
}
Neuron
  • 3,776
  • 3
  • 24
  • 44
1

You should use reflection - init a class object, then a method in this class, and then invoke this method on an object with optional parameters. Remember to wrap the following snippet in try-catch block

Hope it helps!

Class<?> aClass = Class.forName(FULLY_QUALIFIED_CLASS_NAME);
Method method = aClass.getMethod(methodName, YOUR_PARAM_1.class, YOUR_PARAM_2.class);
method.invoke(OBJECT_TO_RUN_METHOD_ON, YOUR_PARAM_1, YOUR_PARAM_2);
nurnachman
  • 4,247
  • 2
  • 31
  • 40
1

This is working fine for me :

public class MethodInvokerClass {
    public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, ClassNotFoundException, InvocationTargetException, InstantiationException {
        Class c = Class.forName(MethodInvokerClass.class.getName());
        Object o = c.newInstance();
        Class[] paramTypes = new Class[1];
        paramTypes[0]=String.class;
        String methodName = "countWord";
         Method m = c.getDeclaredMethod(methodName, paramTypes);
         m.invoke(o, "testparam");
}
public void countWord(String input){
    System.out.println("My input "+input);
}

}

Output:

My input testparam

I am able to invoke the method by passing its name to another method (like main).

Luke H
  • 67
  • 7
Tam
  • 3,153
  • 3
  • 30
  • 50
1

using import java.lang.reflect.*;

public static Object launchProcess(String className, String methodName, Class<?>[] argsTypes, Object[] methodArgs)
        throws Exception {

    Class<?> processClass = Class.forName(className); // convert string classname to class
    Object process = processClass.newInstance(); // invoke empty constructor

    Method aMethod = process.getClass().getMethod(methodName,argsTypes);
    Object res = aMethod.invoke(process, methodArgs); // pass arg
    return(res);
}

and here is how you use it:

String className = "com.example.helloworld";
String methodName = "print";
Class<?>[] argsTypes = {String.class,  String.class};
Object[] methArgs = { "hello", "world" };   
launchProcess(className, methodName, argsTypes, methArgs);
dina
  • 3,060
  • 5
  • 26
  • 52
0

With jooR it's merely:

on(obj).call(methodName /*params*/).get()

Here is a more elaborate example:

public class TestClass {

    public int add(int a, int b) { return a + b; }
    private int mul(int a, int b) { return a * b; }
    static int sub(int a, int b) { return a - b; }

}

import static org.joor.Reflect.*;

public class JoorTest {

    public static void main(String[] args) {
        int add = on(new TestClass()).call("add", 1, 2).get(); // public
        int mul = on(new TestClass()).call("mul", 3, 4).get(); // private
        int sub = on(TestClass.class).call("sub", 6, 5).get(); // static
        System.out.println(add + ", " + mul + ", " + sub);
    }
}

This prints:

3, 12, 1

Andronicus
  • 23,098
  • 14
  • 38
  • 73
0

For those who are calling the method within the same class from a non-static method, see below codes:

class Person {
    public void method1() {
        try {
            Method m2 = this.getClass().getDeclaredMethod("method2");
            m1.invoke(this);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public void method2() {
        // Do something
    }

}
chrizonline
  • 3,969
  • 15
  • 53
  • 91
-10

for me a pretty simple and fool proof way would be to simply make a method caller method like so:

public static object methodCaller(String methodName)
{
    if(methodName.equals("getName"))
        return className.getName();
}

then when you need to call the method simply put something like this

//calling a toString method is unnessary here, but i use it to have my programs to both rigid and self-explanitory 
System.out.println(methodCaller(methodName).toString()); 
lpapp
  • 47,035
  • 38
  • 95
  • 127
SMayne
  • 5
  • 1
  • 4
    If the instance is already known during compiletime, why don't you just do `className.getName().toString()`? You're missing the whole point of reflection. – BalusC Jun 16 '10 at 20:31
  • Like I said, unnecessary in this case, but assuming you'll always know the instance is a bad programming habit. – SMayne Jun 17 '10 at 17:19
  • 2
    @SMayne: I would suggest to delete this post. – lpapp Apr 18 '14 at 15:11
  • bad programming would rather be a compliment in this case – pdenti Dec 12 '14 at 18:57