Accessing array classes

O

ohoehle

Hi!

There is a given object of type java.lang.Class and I want to get the
array class corresponding to my class, aka the Class object with a
componentType identical to the given Class object.

I did it this way...

public static Class getArrayClass(Class componentType) {
return Array.newInstance(componentType, 0).getClass();
}


but creating an array just to get its class is &§&%/()/%$/&%$!!! in my
eyes...

Do you know a better solution?

Regards
Oliver
 
C

Chris Uppal

ohoehle said:
There is a given object of type java.lang.Class and I want to get the
array class corresponding to my class, aka the Class object with a
componentType identical to the given Class object.

This is something of a FAQ (not to mention something of a stupid omission from
java.lang.Class). One discussion of the issue which I remember ('cos I took
part in it -- and because it went memorably and entertainingly wrong after a
while ;-) is here:

http://groups.google.co.uk/group/comp.lang.java.programmer/browse_frm/thread/5b98910e683da220

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,148
Latest member
ElizbethDa
Top