0

I'm trying to use getMethod()

public Method getMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException

What does that the second argument mean? For example, what should i do if i have two parameters to set?

mark
  • 939
  • 2
  • 13
  • 36
  • the problem is that a very few people knows about that strange topic, so they can't name it properly and refers to specific problems.. 1+ anyway – mark Apr 08 '14 at 19:20
  • That's why you and others asked. The question has already been answered in the link above. If you have other questions, ask them. – Sotirios Delimanolis Apr 08 '14 at 19:21
  • Duplicate questions are not bad things, maybe someone will search for `getMethods` and find this question before they think to search for "ellipsis" and so this question will come up. There's no need to write another answer since the other question already has one. – Kevin Panko Apr 08 '14 at 20:38

2 Answers2

1

In Java, you identify methods by name AND signature meaning order, count and types of method parameters. That is what the second argument stand for.

Method overloading means you have at least two (or more) methods with same name, but different signatures. Using Java reflection code, you have to specify the types of parameters. This is done via varargs-argument, for example:

Method m = getMethod("xyz", Integer.class, String.class);

refers to:

{modifiers} {return type} xyz(Integer arg1, String arg2);
Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
1

The second argument specifies a variable argument. This means that you can supply any list of arguments of the specified type. When you access it in the method body it's just an array. It's just syntactic sugar.

final Method method = clazz.getMethod("methodName", String.class, Integer.class);
emd
  • 740
  • 4
  • 12