• 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

array initialized

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Bill Brogdon's book, Exam Cram 2nd Edition, P50:
<code>
1. int counts[];
2. int[] counts;
3. boolean flags [ ];
4. String names[];
5. MyClass[][] things;
6. System.out.println("counts = " + counts);
If lines 1 through 6 were in a method and the method was exected, line 6 would print "counts = null" bexause the array object has not yet been constructed.
</code>
In my mind, any declaration in a method will not be initialized except array elements, but this array object is not still initialized. So I tried to compile it using JDK 1.3, it complained that counts not initialized. Does JDK 1.2 not complain?
Jason
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jason,
Are you sure you copied the example exactly? You'd get an error if you put lines 1 thru 6 into a method; 'counts' is being declared twice. If you remove line 1, then you're right, you get the 'uninitialized' error. Array reference variables are not automatically initialized to 'null' in methods.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Jason Li
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Except for the comment after some lines of codes which i didn't copy and typo like: "bexause", others are from the book. b'cos i only have jdk1.3, i am not sure if it can run by jdk1.2. Right now i got it. Thank you, Jane.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic