Ive read often that an array is a first class object. For all the arrays we use array.length to find out the length of the array. Does that mean that the length is a parameter of an array (object). probably But here's the catch i used the reflection mechanism to list out all the methods and variables in a class (from Core Java volume 1). When I give the array as an input there is no parameter called length!!! how? why?....
faiza haris
Ranch Hand
Joined: Oct 17, 2000
Posts: 173
posted
0
length() method is defined for java.io.File, RandomAccessFile,StringBuffer, String,and other classes , and Arrays i think override this method when used as arrays for String, etc. Can someone clear the doubts??
yogesh sood
Ranch Hand
Joined: Aug 31, 2000
Posts: 108
posted
0
if u r using reflection it will show only public members thats what important to u do it ur self create a class make constructor public then give name of class as input constructor will be displayed but if u dont make it public it will not be shown. in arrays length is protected field so u can't have a look at that via reflectionsince u cannot extend array.
feel free to correct me if im wrong
If its green its biology if its stinkks its chemistry if it has numbers it is Maths and if it doesn't work its TECHNOLOGY
greg clarke
Greenhorn
Joined: Oct 06, 2000
Posts: 25
posted
0
Mary But aren't arrays rather odd objects? They seem odd to me. Here's what Peter van der Linden says in Just Java 1.1 p.158.
Arrays are regarded as objects in Java. Here are some ways in which arrays are like objects:
Because the language says so
Arrays are a reference type
Arrays are always allocated on the heap, never on the stack
Arrays inherit from Object, and support methods such as toString()
Arrays are allocated with the "new" operator
On the other hand, here are some ways arrays are not like objects:
They have a different syntax from other object classes
You can't subclass and extend arrays
You can't define your own methods for arrays
An array can't implement an interface or be cloned.
... The length of an array ... is a data field in the array class. So you get the size of an array by referencing: a.length
I don't know too much about the reflection mechanism. But maybe your assumption that arrays are "first class" objects is the wrong starting point. Also, does the fact that the length of an array cannot change have any affect on the reflection?
faiza From PvdL Just Java 1.1 p.160. The length of an array of String called args is args.length. The length of the ith element of that array is args[i].length(). args.length is the field of the array, length() method.
greg clarke
Greenhorn
Joined: Oct 06, 2000
Posts: 25
posted
0
yogesh How is the array length field protected? That would mean I can only access it if extend the class or am in the same package, neither of which seems to apply to an array. What package is the array class in? The length variable seems to behave like a public final variable to me. Perhaps arrays are just in a class of their own.
greg clarke
Greenhorn
Joined: Oct 06, 2000
Posts: 25
posted
0
Here is more on the matter from Arnold, Gosling and Holmes (The Java Programming Language 3rd ed. p. 299). As usual they are succinct.
An array is an object but has no members. Querying the Class[/] object of an array for fields, methods or constructors will all yield empty arrays. To reflectively create arrays and to get and set the values of elements stored in an array, you can use the static methods of the [i]Array class.
I think it is clear then that you cannot regard arrays as first class objects. They are objects only in certain senses. In practice it means we can use the length field as needed, but we don't regard it as the member variable of a normal class.