GeeCON Prague 2014*
The moose likes Cattle Drive and the fly likes Say 4A: Array initialization confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Say 4A: Array initialization confusion" Watch "Say 4A: Array initialization confusion" New topic
Author

Say 4A: Array initialization confusion

Dean Funk
Ranch Hand

Joined: Apr 20, 2001
Posts: 42
Outside of any method, I did
static String wordNames[] = new String[ 99 ] ;
Inside main(), I did
wordNames[ 0 ] = "Zero" ;
wordNames[ 1 ] = "One" ;
...
The nitpicker said "You have declared the array. You have not initialized it. Initialize the arrays when they are declared.
Use a static initializer."
I thought that 'static String wordNames[]' was the declaration,
and that ' = new String[ 99 ] ;' was the initialization,
then the 'wordNames[ 0 ] = "Zero" ;' was the assignment?
I understand initializing an array when declaring it, as a style issue to keep things together.
I don't understand where the lines are between declaration/initialization/assignment, or how it's not static...???
Richard Boren
Ranch Hand

Joined: Mar 01, 2001
Posts: 233
I believe the
= new String[99]
part is actually instantiating a String array of a given size.
Greg Harris
Ranch Hand

Joined: Apr 12, 2001
Posts: 1012
there is an easier (and much shorter) way to declare an array with multiple objects... if you already know what will be in the array (the strings), then you can declare them all at the same time (hint). when you declare the static array, go ahead and fill it...
i do not want to give too much away, but if you use the shorter method, you will not need to use new String[ xx ]; ... thus you will not need to use wordNames[ x ]; for each one.


what?
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1747
    
    2
The assignment itself has a link to this page on building and accessing arrays:
http://www.javaranch.com/drive/array.html
Amber Woods
Ranch Hand

Joined: Mar 28, 2001
Posts: 111
Hi Dean,
The link that Michael gave will definatley guide you for how to write the initialization of the array. Declaring I think is the part where you write:
static String wordNames[] = new String[ 99 ] ;
but at that time the size of the array is initialized to 99 and the actual initializations to what goes in the array would be when you write:
wordNames[ 0 ] = "Zero" ;
wordNames[ 1 ] = "One" ;
...
As to the difference between initialization and assignments, well I don't think there is much of a difference other than the initialization is usually the first assignment to the variable that you write. Such as:
int x = 1 ; (declaration and initialization/assignment)
x = 5 ; (assignment)
Hope this makes sense because I am not sure I am explaining myself well!
Michael's link will lead you to your answer for the assignemnt


"Happiness is a way to Travel, <b>Not</b> a Destination" -- Unknown
Adam Vinueza
Ranch Hand

Joined: Apr 16, 2001
Posts: 76
To expand a bit on Amber's excellent explanation:
A static initializer is just a bit of language that initializes an array (i.e., puts elements into it) at the same time as the array's declaration. (I suspect that's why the term "static" is used here: initialization isn't drawn out over time.) So when you write
String wordnames[] = new String[ 99 ];
you're just declaring an array of 99 strings; nothing has been put into it as yet. Initializing must then happen later, string by string: I don't know the Javaspeak term for this, but I would call it "dynamic" initializing, since it happens incrementally.
And of course, the link Michael mentions has the skinny on this.
Stuart Goss
Ranch Hand

Joined: Mar 21, 2001
Posts: 169
static String wordNames[] = new String[ 99 ] ;
Aren't all wordNames[i] now initialized to null ?
String wordName[] ; // declared?
String wordName[] = new String[99] ; // initialize?
String wordName[] = ***************; // assign? (something goes here, I'm not saying what )
Anyway, is that correct? (declare/initialize/assign)
bye,
Stuart
 
GeeCON Prague 2014
 
subject: Say 4A: Array initialization confusion