i'm confused with the way java initializes object arrays:
if i declare array objects as such;
JTextField[] arr_field;
arr_field[0] = new JTextField(10);
arr_field[1] = new JTextField(10);
the compiler reports it as an error, but if i declare it as such;
JTextField[] arr_field = {new JTextField(10), new JTextField(10);
then the complier compiles it. shouldn't they be the same? and also,
why can't i initialize array objects as;
JTextField arr_field[3];
for (int i = 0; i < 3; i++) {
arr_field[i] = new JTextField(10);
}
thanks.. -wee
Peter Duniho - 02 Mar 2008 07:23 GMT
> i'm confused with the way java initializes object arrays:
> if i declare array objects as such;
[quoted text clipped - 8 lines]
>
> then the complier compiles it. shouldn't they be the same?
No, they're not the same. In the latter example, you're providing an
initial value for the array and its elements. In the former, the variable
is never initialized, and so referencing any elements in the array is
illegal.
> and also,
> why can't i initialize array objects as;
[quoted text clipped - 3 lines]
> arr_field[i] = new JTextField(10);
> }
Well, you can't use that syntax. But you could write this:
JTextField[] arr_field = new JTextField[3];
for (int i = 0; i < arr_field.length(); i++)
{
arr_field[i] = new JTextField(10);
}
In other words, you can allocate a new array, assign that instance to the
variable, and then access the array through that variable. (You'll note I
also changed the loop termination...I prefer not hard-coding numbers when
not necessary, as it makes the code more reliable and flexible).
See http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html
for more details.
Pete
Lew - 02 Mar 2008 15:10 GMT
> But you could write this:
>
[quoted text clipped - 14 lines]
> http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html
> for more details.
Peter is right except for one tiny typographical glitch - the 'length'
attribute of an array is not a method, so the termination of the loop will be
with
i < arr_field.length;
without the parentheses.
Array initialization or assignment is a two-step process: first assign the
entire array as a block of "empty" storage locations (there are rules about
the initial value of each element), then assign each element. The first step
does not create individual object instances for each slot, but allocates
pointer storage and determines the overall size of the array - the 'length'
attribute.

Signature
Lew
Roedy Green - 02 Mar 2008 08:17 GMT
>i'm confused with the way java initializes object arrays:
>if i declare array objects as such;
see http://mindprod.com/jgloss/array.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Mark Space - 29 Mar 2008 06:25 GMT
Peter is right but I think maybe could have been simpler. For example,
modify your code like this:
> i'm confused with the way java initializes object arrays:
> if i declare array objects as such;
>
> JTextField[] arr_field;
arr_field = new JTextField[2]; // Add this line
> arr_field[0] = new JTextField(10);
> arr_field[1] = new JTextField(10);
A JTextField[] is just one value, the reference. It's just like a 4
byte pointer. It can hold one address, but that's it. You still have
to allocate some space for the array itself.
/* C example */
int *arr_field;
arr_field = malloc( sizeof (int), 2 ); // ? didn't check syntax
Now you can use it arr_field. Before the malloc (= "new") there wasn't
any memory to put your new JTextField(10) into.
I got bit by this when I was first starting out. It seems like in an
object oriented language it should be more automagic, but it's not. You
still have to allocate the array.
> JTextField arr_field[3];
I don't know why this doesn't work. The language designers just decided
not to include it. Use the syntax above with the explicit array allocation.