Ilya
Ilya

Reputation: 29693

Java Class Method, method invoke() and primitive types

Let's I have method

someMethod(int, String)

and I want to invoke it by method invoke(Object, Object[]) of class Method. Can I do it? If I can then how?

Upvotes: 2

Views: 2181

Answers (2)

P Varga
P Varga

Reputation: 20229

You can, use invoke(Instance of Object containing Method, 5, "foo")

Upvotes: 3

AlexR
AlexR

Reputation: 115328

You can do it as following:

MyClass.class.getMethod(int.class, String.class).invoke(obj, 1, "hello")

where obj is a instance of your class. If your method is static you can pass null instead of obj.

Upvotes: 3

Related Questions