• 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

local variables

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!


 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic