Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

length variable of arrays

 
Lalit Bansal
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to know in which class is the "length" variable defined.

Thanks in advance.
 
Kai Witte
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

it is defined in every array type. Arrays do not have a common superclass which defines it. See JLS 10.7

Kai
 
Lalit Bansal
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response.

Actually, I assume that there must be some class where this public variable is defined.
Can you please let me know which is that class?
 
Andy Morris
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
arrays are a special type of object that you wont be able to find the API and see a public 'length' attribute. Wrt the exam, I wouldn't see the point probing much further into this area.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lalit Bansal:
...Actually, I assume that there must be some class where this public variable is defined...

It seems logical to expect a java.lang.Array class in the API, but instead arrays are built into the language itself. The details are documented in Chapter 10 of the Java Language Specification (JLS). In particular, the "length" member is described under 10.7 - Array Members.
[ March 08, 2007: Message edited by: marc weber ]
 
Lalit Bansal
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanking all of you for clarifying on same.

The JLS mentions:
An array thus has the same public fields and methods as the following class:

class A implements Cloneable, java.io.Serializable {
public final int length = X;
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
throw new InternalError(e.getMessage());
}
}
}


Hence, I assume that whenever we declare and initialize an array, it becomes an instance of this class (i.e., class A).

Please correct me if my assumption is wrong.

Thanks.

NB: I know that such in-depth details are not required for the SCJP exam. It was only because of my curiousity I asked the same. Please excuse me for mentioning this problem on wrong forum.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic