This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes length variable of arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "length variable of arrays" Watch "length variable of arrays" New topic

length variable of arrays

Lalit Bansal
Ranch Hand

Joined: Jan 29, 2007
Posts: 63

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

Thanks in advance.
Kai Witte
Ranch Hand

Joined: Jul 17, 2004
Posts: 356

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


Kai Witte's business website Kai Witte's private homepage
Lalit Bansal
Ranch Hand

Joined: Jan 29, 2007
Posts: 63
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

Joined: May 30, 2004
Posts: 78
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

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Lalit Bansal
Ranch Hand

Joined: Jan 29, 2007
Posts: 63
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, {
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.


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.
I agree. Here's the link:
subject: length variable of arrays
Similar Threads
How does .length actually work? How would I go about finding out?
Expression Language Help
About array in Java
How To Make an Array from Multiple Selection of a Drop-Down Menu (or Scrollable List)