Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

local variables

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:-)
------------------
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic