Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes array initialized Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "array initialized" Watch "array initialized" New topic

array initialized

Jason Li
Ranch Hand

Joined: Jun 24, 2001
Posts: 46
In Bill Brogdon's book, Exam Cram 2nd Edition, P50:
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.
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?

green horn
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
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

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Jason Li
Ranch Hand

Joined: Jun 24, 2001
Posts: 46
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.
I agree. Here's the link:
subject: array initialized
It's not a secret anymore!