• 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

problem with local array statement

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
quoting for K&B
page 200

"The array object itself, however, will not
be initialized if it's declared locally. In other words, you must explicitly initialize an
array reference if it's declared and used within a method"



when i type the code
public class te
{
static harsh []d;
public static void main(String []args)
{
harsh[] c = new harsh[10];
for(Object a : c)
System.out.print(a);

}

}
where harsh is a class i already have on my hard-disk. I Agree that it print null but does it not contrast what K&B said
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont think this thing is in contrast to what is written in buk.

You check once again your example.
You are explicitly initializing array in main method.
[ September 09, 2008: Message edited by: bittoo garg ]
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What the book meant was this



the second statement will not compile because arrLocal will not be set to null. But arrays created at class level will be set to null. Whether you create an array in a class or method, it's elements will always be set to null after you intialize the array with = new type[size];
 
AKINLEYE ADEDAMOLA
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much anit i guess where i missed it was i thought when you populate the array then you have intialized it i didn't know using new []<type> was also intialization
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by AKINLEYE ADEDAMOLA:
thank you very much anit i guess where i missed it was i thought when you populate the array then you have intialized it i didn't know using new []<type> was also intialization



It's Ankit....
 
It's a tiny ad only because the water is so cold.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic