• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to use Class.getMethod() by primitive type parameter?

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to use Class.getMethod('methodname',paramA) to load a 'methodname(float)' method,paramA={new Float(13.0)},but when run it, throw a nosuchmethod exception,how JVM distinguish the primitive type parameter and its wrapper?
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you have to use the float.class (not Float.class) when looking up primitive methods that take floats.
Other primitives follow the same format:
int.class, double.class, boolean.class, etc. The only problem with this is that I don't think that it's possible to dynamically determine these class names (i.e., you can't pass in a String and do a Class.forName() on it); instead, you have to have a bunch of if/elses to test the string.
 
fei peng
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks,it work.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's an exercise I did in reflection, inspired by a magazine article with a great idea and code that made me wince. You might step through ThingTester with a debugger and see how it handles int and Integer parameters. I guess I should post a ZIP of the source as well as the HTML version. Something to do tomorrow night. (Kill Bill tonite!) (Next day: Kill Bill rocks!)
Interpreter
[ October 14, 2003: Message edited by: Stan James ]
 
Don't sweat petty things, or pet sweaty things. But cuddle this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic