Create Java method from a string?

Posted by drozzy on Stack Overflow See other posts from Stack Overflow or by drozzy
Published on 2010-06-11T14:27:21Z Indexed on 2010/06/11 14:32 UTC
Read the original article Hit count: 269

Filed under:

Is it possible in Java, to declare a method with a string instead of an identifier?

For example can I do something like the following:

class Car{

     new Method("getFoo", {
          return 1+1;
     });
}

//Use it
Car car = new Car();
car.getFoo();

EDIT: I am adding a Purpose WHY I need this. In order to not hardcode method names when using Jersey and it's UriBuilder, which requires a method name: https://jsr311.dev.java.net/nonav/releases/1.1/javax/ws/rs/core/UriBuilder.html See path() method with signature:

 public abstract UriBuilder path(java.lang.Class resource,
                            java.lang.String method)
                     throws java.lang.IllegalArgumentException

I hope my question is clear, if not - let me know and I can clarify it.

© Stack Overflow or respective owner

Related posts about java