Sorry if this question have been asked before. I have some doubts regarding length field of Object class. Correct me if i am wrong, Every class impilcitly extends Object class thats why we can access every methods like equals,clone,hashcode etc
So my question is when we create any array for example array of int[] ,foo[] we can access length field of Object class but when we create any object we can not see length variable, why?
The Object
class does not define a length
field, and an Object
does not have one.
The length
field is defined for arrays types only. The JLS states:
"In the Java programming language, arrays are objects (§4.3.1), are dynamically created, and may be assigned to variables of type Object (§4.3.2). [...] The array's length is available as a final instance variable length."
The length field is a defined implicitly. There is no Java API class declaration where you can see field
defined. The length
field is typically implemented by "JVM magic"; e.g. in the object header. There is even a special bytecode (arraylength
) to access it.
... when we create any object we can not see length variable, why?
Why? Because it doesn't have one!