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]
# 1

Hello,

As array accesses are checked at runtime, I think that the length attribute is not exactly a field. However, if you want to use the reflection to get the properties of an array, you could try the java.lang.reflect.Array object. It is the object to handle the array properties.

I hope it helps.

giannydamour at 2007-7-1 18:08:23 > top of Java-index,Core,Core APIs...
# 2
Yes, it does sound as though arrays are not "normal" objects, but a special case.
CChrisB at 2007-7-1 18:08:23 > top of Java-index,Core,Core APIs...
# 3

My understanding is that "arrays" are really just a big pile of syntactic sugar for whatever is going on underneath. Unfortunately, this sugar is so heavy that we can't really get at the underlying structure at all.

java.lang.reflect.Array is, as far as I can tell, just a representation of them for reflection purposes, and java.util.Arrays simply provides methods for dealing with it.

If anybody can point me to a class that really does represent arrays, i'd be grateful

shrink_laureate at 2007-7-1 18:08:23 > top of Java-index,Core,Core APIs...
# 4
not sure why, but the javadoc for Class.getFields() does indicate that length is not reflected. oh well.--p
pholser at 2007-7-1 18:08:23 > top of Java-index,Core,Core APIs...
# 5

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

CChrisB at 2007-7-1 18:08:23 > top of Java-index,Core,Core APIs...