12
class Test {
    public static void main(String[] args) throws Exception {
        Test t = new Test();
        System.out.println(t.field);
        System.out.println(t.getClass().getField("field").get(t));

        int[] ar = new int[23];
        System.out.println(ar.length);
        System.out.println(ar.getClass().getField("length").get(ar));

    }
    public int field = 10;
};

When I run the above code, I get the following result on the command line --

10
10
23
Exception in thread "main" java.lang.NoSuchFieldException: length
    at java.lang.Class.getField(Class.java:1520)
    at Test.main(Test.java:9)

How come I am unable to access the field "length" in the array?

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
math4tots
  • 8,540
  • 14
  • 58
  • 95

3 Answers3

10

There is an special java.lang.reflect.Array Class. length is no normal field. To access it there is a special method getLength.

Maccesch
  • 1,998
  • 1
  • 18
  • 27
  • While this does allow reflection to work correctly, I think this doesn't answer the question. The OP's question, at least as I'm reading it, is why the above code fails. – templatetypedef Jun 19 '12 at 09:02
  • 3
    @templatetypedef it actually does. He says that `length` is no normal field, which explains the `java.lang.NoSuchFieldException`... – brimborium Jun 19 '12 at 09:10
  • @brimborium- Is there a citation for why `length` is "no normal field?" According to the JLS array types act as if `length` was declared as a public field, and though the bytecode is different for field lookups I can't find anything specifically saying "array length is different." – templatetypedef Jun 19 '12 at 09:11
  • The answer from brimborium links to a SO thread that provides some citations for this. – Maccesch Jun 19 '12 at 09:13
5

I think this might be a bug in the JVM implementation. Here is my reasoning:

  1. According to the documentation for Class.getField, getField should, as part (1) of its search algorithm, find length if it was declared as a public field: "If C declares a public field with the name specified, that is the field to be reflected."

  2. According to the Java Language Specification, every array has length declared as "The public final field length, which contains the number of components of the array."

  3. Since this field is declared as having name length, getField should either throw a SecurityException as documented, or should return the Field object.

Now interestingly, the Class.getFields method explicitly mentions that "The implicit length field for array class is not reflected by this method. User code should use the methods of class Array to manipulate arrays." This does not seem to parallel getField, so this could either be a misreading on my part or just bad documentation.

Hope this helps!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
0

In Java, arrays are just simple objects. Object don't have any field called length. That's why the reflection is failing.

Please see http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html for more information on how arrays are implemented.

From the documentation ...

An array's length is not part of its type.

sperumal
  • 1,499
  • 10
  • 14