• 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

Introspection - Class object representing an array?!

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey everyone! I am using introspection and would like to get a method using

The method I want to get takes a string array as the parameter type. But I am not sure how to get a Class instance that represents an array so I can use the method call above. Is it even possible? In the past, I have just used getMethods() and found the method by comparing the class names of the parameter types of each method to "[Ljava.lang.string;". But this seems like a huge kludge to me and takes away from the simplicity of the code. Any ideas?
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Class object for a string array is
String[].class .
I was as surprised as you are to find that this is legal Java, which compiles and runs successfully:
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup. There are also class literals for primitive types:
int.class
boolean.class
float.class
The only use I know for these is just what you're doing - specifying parameter/return types for reflection. These are not the same as
Integer.class
Boolean.class
Float.class
(Which can also be used to specify parameter/return types - but different types.)
Even weirder, there's:
void.class
As far as I can tell, the only time this is used is to talk about the return type of a method with reflection. (For parameter lists there's no need - instead of a void parameter, you'd just use a zero-length Class[] array.)
[ August 20, 2002: Message edited by: Jim Yingst ]
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On the other hand, Class.forName("java.lang.String[]") threw an IllegalArgumentException when I tried it.
I started a new discussion thread about this in the Intermediate folder.
 
Everybody's invited. Even 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