• 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

why should local variable initialise before using them, why not for instance variable

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
why should local variable initialise before using them, why instance variable initialises to default values .

thanks
rajendar
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Short answer: because that's how the language was specified.

Anything beyond that is speculation. One guess would be that it's easier to get the compiler to check local variables for initialisation: it's easy to follow the sequence of commands in a method and see that a variable might get used before it is initialised. Whereas an instance variable is much harder. If you had a public getter and a setter, for instance, you might have to analyse the entire application to be able to work out whether the getter could be called before the setter was.
 
reply
    Bookmark Topic Watch Topic
  • New Topic