I have a method that calls it's self (i.e. recursion). I have a local
variable there that for compiler reasons they want initialed (i.e. to
null to start). But when you recall it it makes it null again. static
doe snot seem to work for method variables only class variables. How do
I do this?
Thanks,
Frank
A notional view of what I'm talking about.
class test
{
public void m1()
{
String s;
System.out.println(s);
m1();
}
....
}
Joan - 23 Jun 2005 21:41 GMT
> I have a method that calls it's self (i.e. recursion). I have a local
> variable there that for compiler reasons they want initialed (i.e. to
> null to start). But when you recall it it makes it null again. static
> doe snot seem to work for method variables only class variables. How do
> I do this?
http://danzig.jct.ac.il/java_class/recursion.html
> Thanks,
>
[quoted text clipped - 13 lines]
> ....
> }
sameergn@gmail.com - 23 Jun 2005 22:04 GMT
Local variables are initialized every time function is called.
To fullfil your requirement, you can pass s to ml() as argument.
public void ml(String s)
{
String myStr;
ml(myStr);
}
Nigel Wade - 24 Jun 2005 10:40 GMT
> I have a method that calls it's self (i.e. recursion). I have a local
> variable there that for compiler reasons they want initialed (i.e. to
[quoted text clipped - 19 lines]
> ....
> }
A local variable exists only within a method, and each invocation of the
method will create a new, different instance of that variable. If each
invocation of the method needs to see the same variable then you need a
class instance variable (aka field):
class test
{
String s;
public void m1()
{
System.out.println(s);
m1();
}
....
}

Signature
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : nmw@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555