• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Why to initialize local variables explicitly

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Ranchars,
I want to know, why local variables are must to be initialized explicitly before use. We know that if we don't do that , the compiler will become red.
So what is the benefit the compiler gets if we initialize the local variables explicitly?
Also why we can not leave them as that of instance variables(which gets their default values implicitly if not assigned explicitly).

Please guide...
Thanks
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI. Local variable are called stack/Automatic variabes. These are not instance variables and static variables that have default values.

Local variables dont have implicit default values. So we must initialize it explicitly.
 
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I never knew compilers could change colour.

Local variables usually keep their values on a stack rather than on the heap, so there is a risk that a previous value on the stack will be read rather than a default value. The compiler therefore insists every local variable be explicitly initialised.
 
Md. Mohd
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
I never knew compilers could change colour.

Local variables usually keep their values on a stack rather than on the heap, so there is a risk that a previous value on the stack will be read rather than a default value. The compiler therefore insists every local variable be explicitly initialised.



Thanks Campbell.
( Change in compiler color means the compile time error (doesn't sound good)).
Anyways,
Its true that local variables are stack/automatic variables and lives in the method stack.
It will be great if you please elaborate "a previous value on the stack will be read rather than a default value."

Thanks
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Besides that, I guess the local variables are final. So a value has to be assigned to them. Correct me if I am wrong here
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
I never knew compilers could change colour.


a place i worked put something into the build scripts where if the build failed, your normally white-on-black command window turned black-on-red.
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Besides that, I guess the local variables are final. So a value has to be assigned to them.



it is not right.

If they are final, how we can change the values for a local variable?
 
Ranch Hand
Posts: 179
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As AnoobKumar was talking about all local variables are not final.
 
Shiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic