• 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

Simple declaration of a local variable

 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Folks:
Here is an issue.
"The default initialization does not apply to local reference variables, and therefore does not apply to local arrays as well" From Khalid's book
Makes Perfect Sense!!
But here is my question
When i declare a variable, of primitive type, array or may be even an object reference, and just leave it (ie not initiliaze them) inside a method, (local variable) , the compiler actually
wont complain!!!.Its only when you try to use it, then it complains!!!.
But isnt kinda odd that compiler ignores, the declaration, and wakes up, only when its trying to be used?
What do you guys think? should the compiler enforce the policy of
not to declare a variable without initialization PERIOD, wheather you use it or not?
Please post your thoughts
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why should the compiler complain? It may be that it isn't initialized until later in your code...
int a;
// bunch of code
a = getValueFromDB();
if (a > 100)
// do stuff
Now what if I change the method and delete every reference to a? Why should the compiler complain?
It is good practice to always initialize your method variables when you define them but it isn't required.

------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but i thought that default initialization takes place for arrays irrespective of whether they are member variables or local variable.
in fact the following code works just fine
and prints 0
class test2 {
public static void main(String [] args)
{
int arr[]=new int[4];
System.out.println(arr[1]);
}
}
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Namrata
"The default initialization does not apply to local reference variables, and therefore does not apply to local arrays as well" From Khalid's book
The above explanation refers to array variable only
ie
public static void main (String args[]) {
int a [];
System.out.println(a);
}
Compiler will complain becoz a[] which is local doesnt have
a default initialization to null, which is possible if it were
a class member

But in your example , you had already declared and initialized to a fixed size. So it wont complain and gives value 0 since it is the default value of the int array elements wheather its local or class member
HTH
 
reply
    Bookmark Topic Watch Topic
  • New Topic