Why doesn't an array's .length show in reflect?
Why does an arrays .length not show in reflect?
After reading charliesildavia s post and seeing a program which displayed data about an arrays class, I realised that my reply there is a .length field was over simplistic. The following program shows the fields in an object:
// Copyright (c) 2001 Yorkshire Building Society
package package11;
publicclass ShowFieldsextends Object{
// Based on original program on http://c2.com/cgi/wiki?JavaArraysShouldBeFirstClassObjects
publicstaticvoid main(String[]args){
// Integer x = new Integer(4) ; // uncomment this line and comment following one to show that filed printing works
int[] x ={1, 2} ;
java.lang.reflect.Field[] fields= x.getClass().getFields();
System.out.println("Begin listing fields");
for (int i= 0; i < fields.length; i++)
System.out.println("\t" + fields[i]);
System.out.println("End listing fields");
}
}
The output from this program is:
Begin listing fields
End listing fields
The program works fine for ordinary objects, so why is thelength object not displayed? Is the array'slength field some special case in the Java language, and not a real field?
[2023 byte] By [
CChrisB] at [2007-9-26 7:54:26]

Actually its worse than that. If you initialise an object like:
Object x = new int[] {1, 2} ;
Then reflection will say that it has ho methods. Even so calling say toString() etc - the methods an object should have works!
If you initialise the Object with
Object x = new Object() ;
or any other normal object, then they are listed.
The test code is:
public class ShowFields extends Object {
// Based on original program on http://c2.com/cgi/wiki?JavaArraysShouldBeFirstClassObjects
public static void main(String[]args) {
//Object x = new Object() ; // uncomment this line and comment following one to show that filed printing works
Object x = new int[] {1, 2} ;
java.lang.reflect.Field[] fields= x.getClass().getFields();
System.out.println("Beging listing fields");
for (int i= 0; i < fields.length; i++)
System.out.println("\t" + fields[i]);
System.out.println("End listing fields");
//Listing method
java.lang.reflect.Method[] methods= x.getClass().getMethods();
System.out.println("Begin listing methods");
for (int i= 0; i < methods.length; i++)
System.out.println("\t" + methods[i]);
System.out.println("End listing methods");
// now try calling the object methods
System.out.println("exercise toString(): " + x.toString());
System.out.println("exercise equals(Object): "+ (x.equals(new int []{1,2})) );
}
}
Output with = new int[] {1, 2} ;
Beging listing fields
End listing fields
Begin listing methods
End listing methods
exercise toString(): [I@f69
exercise equals(Object): false
Object x = new Object() ;
Beging listing fields
End listing fields
Begin listing methods
public boolean java.lang.Object.equals(java.lang.Object)
public final native java.lang.Class java.lang.Object.getClass()
public native int java.lang.Object.hashCode()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
public java.lang.String java.lang.Object.toString()
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
End listing methods
exercise toString(): java.lang.Object@f6a
exercise equals(Object): false