Someone please help to understand how can we initialize array in java using reflection.
for a simple object we can do like this :
Class l_dto_class = Class.forName(p_fld.getType().getName());
Object l_dto_obj= l_dto_class.newInstance();
but for the case of array it is giving me exception.
java.lang.InstantiationException
You can instantiate array like this:
if (l_dto_class.isArray()) {
Object aObject = Array.newInstance(l_dto_class, 5); //5 is length
int length = Array.getLength(aObject); // will be 5
for (int i=0; i<length; i++)
Array.set(aObject, i, "someVal"); // set your val here
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With