Getting the class of an n dimensional array of an runtime supplied class name
Posted
by
MeBigFatGuy
on Stack Overflow
See other posts from Stack Overflow
or by MeBigFatGuy
Published on 2011-01-03T00:53:10Z
Indexed on
2011/01/03
5:53 UTC
Read the original article
Hit count: 227
Given a fully qualified class name, and a number of dimensions, i would like to get the Class name for this class. I believe i can do this like such
public Class<?> getArrayClass(String className, int dimensions) throws ClassNotFoundException {
Class<?> elementType = Class.forName(className);
return Array.newInstance(elementType, new int[dimensions]).getClass();
}
However this requires me to create an unneeded instance of the class. Is there a way to do this without creating the instance?
It does not appear that Class.forName("[[[[Ljava/lang/String;") (or a algorithmically generated version) works correctly in all instances from various blog posts i've seen.
© Stack Overflow or respective owner