Java Reflection field is array

Introduction

To identify if a class corresponds to an array using static isArray() method of Class.

The following program retrieves all array fields of a specified class:

import java.lang.reflect.Field;

public class Main {
   public static void main(String args[]) throws Exception {
      Class c = Class.forName("java.lang.String");
      Field[] fields = c.getDeclaredFields();
      for (Field f : fields)
         if (f.getType().isArray())
            System.out.println(f);
   }/* ww  w  . j ava  2  s .co  m*/
}



PreviousNext

Related