Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

local variable recersion inital izing problem

Thread view: 
frank - 23 Jun 2005 21:12 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?

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.