Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java in General
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Liutauras Vilda
Ron McLeod
Sheriffs:
Jeanne Boyarsky
Devaka Cooray
Paul Clapham
Saloon Keepers:
Scott Selikoff
Tim Holloway
Piet Souris
Mikalai Zaikin
Frits Walraven
Bartenders:
Stephan van Hulst
Carey Brown
Forum:
Java in General
Class.getMethod with variable args
Stephen Upton
Greenhorn
Posts: 9
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi,
I'm struggling to find the right syntax to invoke a method using reflection that uses variable argument list, e.g.,
class A { public void myMethod(int x, String... otherargs) {...} etc.... }
then
Method m = AasClass.getMethod("myMethod", ???); m.invoke(Ainstance, ???);
hope that's clear
thank you
steve
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
I like...
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi Stephen,
Welcome to JavaRanch!
varargs is just syntactic sugar for a final argument of array type; i.e., the "real" declaration of your method is
public void myMethod(int x,
String
[] otherargs) {...}
So you want...
Method m = AasClass.getMethod("myMethod", new Class[] {Integer.TYPE, String[].class});
[Jess in Action]
[AskingGoodQuestions]
Jim Yingst
Wanderer
Posts: 18671
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Or since getMethod() now uses varargs itself, you can get rid of the "new Class[]" part and just use:
Method m = AasClass.getMethod("myMethod", Integer.TYPE, String[].class);
"I'm not back." - Bill Harding,
Twister
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
I like...
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
[Jess in Action]
[AskingGoodQuestions]
Stephen Upton
Greenhorn
Posts: 9
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thanks for the welcome - and the answer!
Time is mother nature's way of keeping everything from happening at once. And this is a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Creating new object
Boolean value not changing
Question on Anonymous Inner class
Varargs
check return type of method
More...