• 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

Initialization of local variables

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
can you explain the following statement and give examples:
--------------------------------------------------------------
The Rule is : initialization of a local variable must be done explicitly before the value is used .
---------------------------------------------------------------
Thanks in advance.
Thomas
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
member variables get default value if u do not assign a value explicitly depending on the type of variable whereas local variables have to be assigned a value explicitly.
for eg.,
1.
public class test
{
int i; //member variable
public static void main(String []args)
{
int j; //local variable
System.out.println(i); //will compile correctly
System.out.println(j); //will give a compile error
}
}
2.
public class test
{
int i; //member variable
public static void main(String []args)
{
int j; //local variable
i=j + 1; //will give a compile error since 'j'is being used without explicitly assigning it a value

j= i +1; no problem since i being a member variable is assigned a default value of 0
}
}
hope this helps!!
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's very well explained Namrata
-- Sandeep
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Thomas,
I must add one thing to the Namarta's post that local variables have to be assigned a value unconditionally before use.
Gurpreet
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gurpreet Sachdeva:
Hello Thomas,
I must add one thing to the Namarta's post that local variables have to be assigned a value [b]unconditionally
before use.
Gurpreet[/B]


Excluding the cases where the conditional operator is used.

------------------
Antti Barck
It Solutions Consultant, NSD Oy
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is actually a reason why instance variables are initialized a method variables aren't. Originally, no variables were going to be given a default value but this opened up several problems. In the case of an instance variable, how would the compiler know if it was initialized prior to use? It could be that it is used in one method but initialized in another method. There could be a requirement to initialize all variables in every constructor (much as we do for final variables) but then what do we do with classes that have only the default constructor? In order to bypass these issues, it was decided to supply default values for instance variables.
But what about method variables? None of these issues exist. The compiler can easily tell if the method variable has been initialized prior to use so there is no reason to supply a default value.
------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums
 
Trust God, but always tether your camel... to this tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic