How to create an instance of a class which is actually an array?

the following code gives a runtime exception

Object obj = (Object )attributeClass.newInstance();

Exception:

java.lang.InstantiationException: [Ltest.Name;

Here test.Name is user defined class and i want to create an array instance of that class.

I have tried the following also:

Object methodArgs[] = new Object[length];

for(int j=0;j<length;++j){

methodArgs[j] = singleMemberClass.cast(testArray[j]);

}

Object temp = attributeClass.cast(methodArgs);

In the above codesingleMemberClass is test.Name, but the last line gives the following exception.

java.lang.ClassCastException

Message was edited by:

lalit_mangal

Message was edited by:

lalit_mangal>

[797 byte] By [lalit_mangala] at [2007-10-3 3:22:23]
# 1

Try the following code

import java.lang.reflect.Array;

public class TestReflection {

public static void main(String args[]) {

Object array = Array.newInstance(A.class, 3);

printType(array);

}

private static void printType(Object object) {

Class type = object.getClass();

if (type.isArray()) {

System.out.println("Array of: " + elementType);

System.out.println("Array size: " + Array.getLength(object));

}

}

}

class A{

}

Vchutkia at 2007-7-14 21:14:52 > top of Java-index,Core,Core APIs...
# 2
Thanks a lot!!! I think this will solve my problem.
lalit_mangala at 2007-7-14 21:14:52 > top of Java-index,Core,Core APIs...