5

How can we call a method which name is string at runtime. Can anyone show me how to do that in Java and C.

1
  • This cannot be done in C. C doesn't have methods. Commented May 21, 2010 at 14:31

7 Answers 7

26

In java it can be done through the reflection api.

Have a look at Class.getMethod(String methodName, Class... parameterTypes).

A complete example (of a non-static method with an argument) would be:

import java.lang.reflect.*;
public class Test {

    public String methodName(int i) {
        return "Hello World: " + i;
    }

    public static void main(String... args) throws Exception {
        Test t = new Test();
        Method m = Test.class.getMethod("methodName", int.class);
        String returnVal = (String) m.invoke(t, 5);
        System.out.println(returnVal);
    }
}

Which outputs:

Hello World: 5

Sign up to request clarification or add additional context in comments.

Comments

3

In Java you would use reflection:

Class<?> classContainingTheMethod = ...; // populate this!
Method stringMethod = classContainingTheMethod.getMethod("string");
Object returnValue = stringMethod.invoke(null);

This is a very simple case that assumes your method is static and takes no parameters. For non-static methods, you'd pass in the instance to invoke the method on, and in any case you can pass in any required parameters to the invoke() method call.

Comments

3

Here is a base C example, I hope it will help you.

typedef void (*fun)(void);

static void hello()
{
  puts("hello world");
}

static void string()
{
  puts("string");
}

static void unknown()
{
  puts("unknown command");
}

struct cmd 
{
  char* name;
  void (*fun) (struct cmd* c);
};

static struct cmd commands[] = {
  { "hello", hello },
  { "string", string },
  { 0, unknown }
};


static void execute(const char* cmdname)
{
  struct cmd *c = commands;

  while (c->name && strcmp (cmdname, c->name))
    c++;
  (*c->fun) (c);
}

int main()
{
  execute("hello");
  execute("string");
  execute("qwerty");
}

1 Comment

That's still a function and not a method, though.
2

In Java:

If class A has a method "string()" then you call it by:

A a = new A();
a.string();

C does not have methods and you can't call them. You may be thinking of C++ which essentially has exactly the same syntax.

4 Comments

"in runtime" suggest that the name of the method is not determined in compile time.
I somehow don't think that's what the OP meant. I understand the question as dynamic function resolution - thus, reflection in Java, and not quite but close, function pointer lookup table in C.
Probably not. But I thought I'd add the exact answer to the question he asked, in case it was what he wanted.
should we not use a = a.string();
2

In Java, you'll have to use the Java Reflection API to get a reference to the Method object representing your method, which you can then execute.

Comments

2

In C (or C++) real reflection is not possible as it is a compiled language.

The most used is to have an associative container (a map) that can link a function name (as a string) to a function pointer. You have to fill in the map in the program with the value you want. This can't be done automatically.

You could also simply have a function that takes a string as parameter and then chose the right function to call with hand-made ifs.

Comments

0

I'm quite sure you can put all your functions into the shared library and load them with dlopen + dlsym.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.