This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes local variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "local variables" Watch "local variables" New topic
Author

local variables

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,have a look at the foll.code:
int sqrroot(int i)
{
int p;//1
if(i>0)
{ p=(int) Math.sqrt(i);//2
}
return p;//3
}
when i compile the class , its giving error as p not initialised
at 3.If p is initailised anywhere above if and after //1 then no
error occurs.I know that local variables must be initialised
when they are declared since they dont have default values.
But,why the compiler doesnt consider initialising of local
variable in a block.
Thanks!
PREETI BHANDARI
Greenhorn

Joined: Jul 28, 2000
Posts: 3
according to me variables defined within a method are local variables and their scope is limited only to the scope of the method or block in which they are declared.
what happens is that when a class is instantiated, the instance calls for the default constructor if no other constructor is explicitly defined in the coding. instance variables defined in the class are considered to be taken within the constructor and a constructor initialises all the uninitialised instance variables to zero. thus variables defined by a method or a block can't become a part of the constructor and therefore are not initialised to zero.
for eg.
class A
{
int i,j;
void getValues()
{
int a=20;
int b=30;
int c=a+b;
System.out.println("the values of a and b are :"+a+" "+b);
System.out.println("the total is :"+c);
}


public static void main(String[] args)
{
A obj=new A();
System.out.println("the default values of i and j are :"+obj.i+" "+obj.j);
obj.getValues();
}
}
here what happens is that when we write
A obj=new A();
the A() goes and looks for a matching constructor as we are making a call to a constructor with no parameters.but since it does not find any match as none is explicitly declared, it calls implicitly the default constructor and the instance variables
int i,j become members of this default constructor and are initialised to zero.
but again if we have explicitly declared a constructor with or without parameters it will look for a match accordingly at the point of call.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks! for your detailed reply.But, my question was regarding
initialising local variables.Why is that local variable
initialising cant be done in a block like in if .
Thanks!
Matt Siegel
Ranch Hand

Joined: Jul 18, 2000
Posts: 55
if the "if" statement is never run, i <=0, then p is never initialized. The compiler will complain because it sees that p might be returned without ever getting a value.<br /> Matt<br />
Originally posted by avn:<br /> Hi,have a look at the foll.code:<br /> int sqrroot(int i)<br /> {<br /> int p;//1<br /> if(i>0)
{ p=(int) Math.sqrt(i);//2
}
return p;//3
}
when i compile the class , its giving error as p not initialised
at 3.If p is initailised anywhere above if and after //1 then no
error occurs.I know that local variables must be initialised
when they are declared since they dont have default values.
But,why the compiler doesnt consider initialising of local
variable in a block.
Thanks!

Thillai Sakthi
Ranch Hand

Joined: Jun 17, 2000
Posts: 102
Another approach to the problem.
We know that instance variables can only be accessed thru' objects and each object of the class will have a separate copy of the instance variables. As class is only a template and objects are the physical realities of the class these objects have these "copy" variables whose initial variables will be set to default values. But the local variables inside a method need not be accessed thru' an object and they are used directly. Hence the compiler demands you to initialise such variables to start with.
Hope I clarified if not confused:-)
------------------

Regards,
Shakthi
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
i think there is no need to make life tough by telling what you know(though that may not have slightest of relevance to the question).
The simple answer is that there is a conditional initialisation of the varable p. for the cases the condition is not satisfied the variable goes uninitialized and that is what irks the compiler, keep the initialisation unconditional or make it both ways
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: local variables
 
Similar Threads
Why variables created in Stack are not initialized
ques from khalid's mock
local arrays
Variable initialization
A beginner 's question