In which class is the length field defined in Java (e.g. for array length)?
Will I be able to see it defined say in Object class?
EDIT : Why was this field so designed(any thing related with security or memory efficiency)?
Array types are special in Java. This is an excerpt from JLS 10.7 Array Members
The members of an array type are all of the following:
- The
public finalfieldlength, which contains the number of components of the array (length may be positive or zero).- The
publicmethodclone, which overrides the method of the same name in classObjectand throws no checked exceptions. The return type of the clone method of an array typeT[]isT[].- All the members inherited from class Object; the only method of Object that is not inherited is its
clonemethod.
Despite this, there are still old-standing bugs when you're using reflection on arrays: neither length nor clone could be found through reflection (bug# 5047859, bug# 4987375).
Neither member is inherited in the traditional way from any superclass; all array types extends from Object directly. This "special treatment" is likely why these bugs exist in the first place.
"does this mean that never will one be able to see the length variable being defined?"
There is no actual source code for array types. Again, these types are special; the JVM just pulls out these types out of a hat whenever they're required. You will not see a .java source file for the array type int[].class (i.e. the type of all int array).
So, no, you will not be able to see the length field defined.
For further reading, here are some information on Java Reflection:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With