Friday, 22 September 2017

Reflection in Java - Array

Array in Java is also a class so many of the methods in java.lang.Class may be used with the array.

Reflection in Java also provides a specific class java.lang.reflect.Array for arrays.

How to identify array using reflection

If you want to determine if a class member is a field of array type that can be done by invoking Class.isArray() method.

Example code

For the example let’s say we have a class TestClass which has one array field numArray. In another class ReflectArray using the class.isArray() method it is determined if there is any array in the TestClass.


public class TestClass {
 private int value;
 private int[] numArray;
 public int getValue() {
  return value;
 public void setValue(int value) {
  this.value = value;
 public int[] getNumArray() {
  return numArray;
 public void setNumArray(int[] numArray) {
  this.numArray = numArray;


import java.lang.reflect.Field;

public class ReflectArray {

    public static void main(String[] args) {
        try {
            Class<?> c = Class.forName("org.netjs.prog.TestClass");
            Field[] fields = c.getDeclaredFields();
            for (Field f : fields) {
                Class<?> type = f.getType();
                // Looking for array
                    System.out.println("Array found " + f.getName());
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block


Array found numArray

Creating new array using reflection

Using reflection API in Java you can dynamically create arrays of arbitrary type and dimensions using java.lang.reflect.Array.newInstance() method.

Example code

int[] testArray = (int[])Array.newInstance(int.class, 5);
System.out.println("length of array " + testArray.length);


length of array 5

Getting and setting array using reflection

Using reflection you can get or set an entire array. You can set an entire array using Field.set(Object obj, Object value) method. To retrieve an entire array use Field.get(Object obj) method.

If you want to get or set an array individual element by element, Array class has methods for that also. In order to set or get an int array you can use setInt(Object array, int index, int i) and getInt(Object array, int index) methods. Same way if you have a float array you can use setFloat(Object array, int index, float f) and getFloat(Object array, int index) methods.

There is also a method which takes Object for value as parameter that can be used with any type - set(Object array, int index, Object value) and get(Object array, int index) methods.

Example code

int[] testArray = (int[])Array.newInstance(int.class, 5);
System.out.println("length of array " + testArray.length);
// Setting values using setInt and set methods
Array.setInt(testArray, 0, 1);
Array.set(testArray, 1, 2);
Array.setInt(testArray, 2, 3);

// Getting values using getInt and get methods
System.out.println("First element " + Array.get(testArray, 0));
System.out.println("Second element " + Array.getInt(testArray, 1));
System.out.println("Third element " + Array.getInt(testArray, 2));


length of array 5
First element 1
Second element 2
Third element 3

That's all for this topic Reflection in Java - Array. If you have any doubt or any suggestions to make please drop a comment. Thanks!

Related Topics

  1. Reflection in Java - Field
  2. Reflection in Java - Method
  3. Reflection in Java - Constructor
  4. Generating getters and setters using Reflection - Java Program
  5. Invoking getters and setters using Reflection - Java Program

You may also like -

>>>Go to Java Advance Topics Page

1 comment:

  1. A easy and exciting blog about java learning. Please comment your opinions and share..