• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Say 4A: Array initialization confusion

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...???
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe the
= new String[99]
part is actually instantiating a String array of a given size.
 
Ranch Hand
Posts: 1012
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The assignment itself has a link to this page on building and accessing arrays:
http://www.javaranch.com/drive/array.html
 
Ranch Hand
Posts: 111
jQuery Oracle C++
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic