• 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

Initializing variables

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
When I do not initialize the reference variable in the below codes, there are a compile time error occured.
the reference variable that I mention : IntBag b
So Why ??

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compile time errors just tells you that b may not have been initialised, this is to tell you that if you run the program there's likely going to be some problems because it's going to try calling a method with a null reference pointing nowhere, and hence won't work. There's no point compiling something that's not going to do what's intended.
[ February 05, 2006: Message edited by: Dani Atrei ]
 
memati bas
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I heard that Java does not automatically initialise local variables, though it will initialise any arrays or objects newly created to be stored in local variables. but the code that I sent deny this.
Why???
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java does not automatically initialize local variables. I don't believe that there is an exception for arrays and objects.

Henry
 
memati bas
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:
Java does not automatically initialize local variables. I don't believe that there is an exception for arrays and objects.

Henry



You should visit the following link.
http://mindprod.com/jgloss/initialisation.html
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by memati bas:


You should visit the following link.
http://mindprod.com/jgloss/initialisation.html



The article did *not* say that arrays (that are local) will get automatically initialized. It mentioned that when you initialize the local array variable, Java will automatically initialize the elements, of that array.

Henry
 
memati bas
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, thanks for your reply.
Now , I kept the idea.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by memati bas:
Hi,
I heard that Java does not automatically initialise local variables, though it will initialise any arrays or objects newly created to be stored in local variables. but the code that I sent deny this.
Why???



In this case b is indeed a local variable (therefore not initialized to anything) and there is no "object newly created" hence the error.
 
I would challenge you to a battle of wits, but I see you are unarmed - shakespear. Unarmed tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic