dimanche 6 mars 2016

Java Reflection on an Integer Object

Given the following Java code:

int x[] = new int[3];

Integer y[] = new Integer[3];

java.lang.reflect.Array.setInt(x, 0, 10); // works as expected (i.e. x[0] = 10) java.lang.reflect.Array.setInt(x, 1, 20); // works as expected (i.e. x[1] = 20) java.lang.reflect.Array.setInt(x, 2, 30); // works as expected (i.e. x[2] = 30)

java.lang.reflect.Array.setInt(y, 0, 10); // throws an exception

java.lang.reflect.Array.setInt(y, 1, 20); // throws an exception

java.lang.reflect.Array.setInt(y, 2, 30); // throws an exception

Why one can use reflection to assign values to variable "x", while it is not possible to do it for variable "y"?





Aucun commentaire:

Enregistrer un commentaire