aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Simple declaration of a local variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Simple declaration of a local variable" Watch "Simple declaration of a local variable" New topic
Author

Simple declaration of a local variable

Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
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
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Namrata Shetty
Ranch Hand

Joined: Jul 18, 2001
Posts: 30
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

Joined: Jul 20, 2001
Posts: 464
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple declaration of a local variable