• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Class.getMethod with variable args

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm struggling to find the right syntax to invoke a method using reflection that uses variable argument list, e.g.,


then


hope that's clear
thank you
steve
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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});
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or since getMethod() now uses varargs itself, you can get rid of the "new Class[]" part and just use:
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
 
Stephen Upton
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the welcome - and the answer!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!