Local variable in recursion retaining values?









up vote
1
down vote

favorite












Currently working on recursion practice/implementation and noticed something that goes against everything I know to be true in programming.



Recursive method



protected int getArea() 

if(width <= 0)
return 0;
else if(width == 1)
return 1;
else

Triangle t2 = new Triangle(width - 1);
int area = t2.getArea();//Area variable somehow is holding the values of previous calls, despite being instantiated in each new call?

return area + width;






Somehow, the local variable, area, is aggregating values from previous calls in the recursive method. How is this possible, when with each call it is instantiated? In each call, it appears the method getArea() is called again, preventing the area variable from holding anything due to the fact the getArea() call happens before the methods return statement.



How is this happening?










share|improve this question























  • "Somehow"? You're telling it to. Get out some paper and a pencil and play computer for a relatively small value. Or use a debugger, but writing it down helps it sink in.
    – Dave Newton
    Nov 10 at 6:50










  • this is not recursive program, what is the program requirement.
    – vels4j
    Nov 10 at 6:59










  • @vels4j how is it not recursive?
    – Matthew
    Nov 10 at 7:08










  • @DaveNewton Thanks. I was just very confused how a newly instantiated variable can hold previous values.
    – Matthew
    Nov 10 at 7:09






  • 1




    @vels4j Pretty sure it's recursive.
    – Dave Newton
    Nov 10 at 7:38














up vote
1
down vote

favorite












Currently working on recursion practice/implementation and noticed something that goes against everything I know to be true in programming.



Recursive method



protected int getArea() 

if(width <= 0)
return 0;
else if(width == 1)
return 1;
else

Triangle t2 = new Triangle(width - 1);
int area = t2.getArea();//Area variable somehow is holding the values of previous calls, despite being instantiated in each new call?

return area + width;






Somehow, the local variable, area, is aggregating values from previous calls in the recursive method. How is this possible, when with each call it is instantiated? In each call, it appears the method getArea() is called again, preventing the area variable from holding anything due to the fact the getArea() call happens before the methods return statement.



How is this happening?










share|improve this question























  • "Somehow"? You're telling it to. Get out some paper and a pencil and play computer for a relatively small value. Or use a debugger, but writing it down helps it sink in.
    – Dave Newton
    Nov 10 at 6:50










  • this is not recursive program, what is the program requirement.
    – vels4j
    Nov 10 at 6:59










  • @vels4j how is it not recursive?
    – Matthew
    Nov 10 at 7:08










  • @DaveNewton Thanks. I was just very confused how a newly instantiated variable can hold previous values.
    – Matthew
    Nov 10 at 7:09






  • 1




    @vels4j Pretty sure it's recursive.
    – Dave Newton
    Nov 10 at 7:38












up vote
1
down vote

favorite









up vote
1
down vote

favorite











Currently working on recursion practice/implementation and noticed something that goes against everything I know to be true in programming.



Recursive method



protected int getArea() 

if(width <= 0)
return 0;
else if(width == 1)
return 1;
else

Triangle t2 = new Triangle(width - 1);
int area = t2.getArea();//Area variable somehow is holding the values of previous calls, despite being instantiated in each new call?

return area + width;






Somehow, the local variable, area, is aggregating values from previous calls in the recursive method. How is this possible, when with each call it is instantiated? In each call, it appears the method getArea() is called again, preventing the area variable from holding anything due to the fact the getArea() call happens before the methods return statement.



How is this happening?










share|improve this question















Currently working on recursion practice/implementation and noticed something that goes against everything I know to be true in programming.



Recursive method



protected int getArea() 

if(width <= 0)
return 0;
else if(width == 1)
return 1;
else

Triangle t2 = new Triangle(width - 1);
int area = t2.getArea();//Area variable somehow is holding the values of previous calls, despite being instantiated in each new call?

return area + width;






Somehow, the local variable, area, is aggregating values from previous calls in the recursive method. How is this possible, when with each call it is instantiated? In each call, it appears the method getArea() is called again, preventing the area variable from holding anything due to the fact the getArea() call happens before the methods return statement.



How is this happening?







java recursion objectinstantiation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 16:00









Mikhail Kholodkov

3,66252343




3,66252343










asked Nov 10 at 6:48









Matthew

18213




18213











  • "Somehow"? You're telling it to. Get out some paper and a pencil and play computer for a relatively small value. Or use a debugger, but writing it down helps it sink in.
    – Dave Newton
    Nov 10 at 6:50










  • this is not recursive program, what is the program requirement.
    – vels4j
    Nov 10 at 6:59










  • @vels4j how is it not recursive?
    – Matthew
    Nov 10 at 7:08










  • @DaveNewton Thanks. I was just very confused how a newly instantiated variable can hold previous values.
    – Matthew
    Nov 10 at 7:09






  • 1




    @vels4j Pretty sure it's recursive.
    – Dave Newton
    Nov 10 at 7:38
















  • "Somehow"? You're telling it to. Get out some paper and a pencil and play computer for a relatively small value. Or use a debugger, but writing it down helps it sink in.
    – Dave Newton
    Nov 10 at 6:50










  • this is not recursive program, what is the program requirement.
    – vels4j
    Nov 10 at 6:59










  • @vels4j how is it not recursive?
    – Matthew
    Nov 10 at 7:08










  • @DaveNewton Thanks. I was just very confused how a newly instantiated variable can hold previous values.
    – Matthew
    Nov 10 at 7:09






  • 1




    @vels4j Pretty sure it's recursive.
    – Dave Newton
    Nov 10 at 7:38















"Somehow"? You're telling it to. Get out some paper and a pencil and play computer for a relatively small value. Or use a debugger, but writing it down helps it sink in.
– Dave Newton
Nov 10 at 6:50




"Somehow"? You're telling it to. Get out some paper and a pencil and play computer for a relatively small value. Or use a debugger, but writing it down helps it sink in.
– Dave Newton
Nov 10 at 6:50












this is not recursive program, what is the program requirement.
– vels4j
Nov 10 at 6:59




this is not recursive program, what is the program requirement.
– vels4j
Nov 10 at 6:59












@vels4j how is it not recursive?
– Matthew
Nov 10 at 7:08




@vels4j how is it not recursive?
– Matthew
Nov 10 at 7:08












@DaveNewton Thanks. I was just very confused how a newly instantiated variable can hold previous values.
– Matthew
Nov 10 at 7:09




@DaveNewton Thanks. I was just very confused how a newly instantiated variable can hold previous values.
– Matthew
Nov 10 at 7:09




1




1




@vels4j Pretty sure it's recursive.
– Dave Newton
Nov 10 at 7:38




@vels4j Pretty sure it's recursive.
– Dave Newton
Nov 10 at 7:38












3 Answers
3






active

oldest

votes

















up vote
3
down vote



accepted










Each method call details are stored on stack, once the value is returned from the method call the execution returns to the previous method calling the current method, the local variable values,etc,. would be stored on the stack and that's how those values are used in the execution when required by program. Do some experimentation around recursive programs to understand more about it.



My suggestion would be try debugging recursive programs using break points on IDE's like eclipse or Intellij, it would clear a lot of confusion and brings clarity on how recursion works.






share|improve this answer


















  • 1




    Thanks for the help hrudayanath, will definitely take a look at it w/ the debugger
    – Matthew
    Nov 10 at 7:10

















up vote
1
down vote













When it comes to recursion, I often find it helpful to remember that what you give is what you get, meaning that what you get when you call your recursive method is whatever you decide to return from it.



In this case, what you get when you write



int area = t2.getArea();


is either 0, 1 or area + width.



The last case is the recursive case where you recursively define a new Triangle instance with the new width decremented 1 and then call .getArea() on that. That is functionally equivalent to just defining .getArea() as



protected int getArea(int width) 

if(width <= 0)
return 0;
else if(width == 1)
return 1;
else
int area = getArea(width - 1);
return area + width;





It makes no difference whether you call .getArea() from one or the other instance of Triangle. All that matters is what you define width to be when calling it (in this case width - 1) and how you define its influence on the return value.






share|improve this answer





























    up vote
    0
    down vote













    I think you are looking at this wrong. If you call two methods. eg.



    public int test() 
    int x = getSomeInt(1);
    int y = getSomeInt(2);
    return x + y;



    Are you ever questioning whether return x + y is done or if the value of x is determined before y? It does it from top to bottom and the statement setting y does not start before getSomeInt(1) has returned and its value set as x.
    So to your example:



    protected int getArea() 
    if (width <= 0)
    return 0;
    elseif (width == 1)
    return 1;
    else
    Triangle t2 = new Triangle(width - 1);
    int area = t2.getArea();
    return area + width;




    So if you have a Triangle with width 1 and call getArea you get 1 back.



    What happens if you do it on a Triangle with width 2? Well it creates t2 with width 1 and call getArea on it. We already know the result since we calculated it already. area becomes 1 and then it returns 1 + 2.



    What happens if you do it with width 3?. It will create t2 with width 2 and call getArea() on that. We know that returns 3 from the above and the result is 3 + 3.



    The recusive method gets called with a high with, but it is the one with 1 that gets determined first, then 2, 3, 4, ...and finally the call you actually called has some area that it adds its with to.



    Each call to a methoid has nothing to do with the callee. It is the same code yes, but it's a different object and the local variables are unique to the call just as the two calls getSomeInt also have two differen versions of whatever it called its first parameter. They are not entangled unless you are mutating or passing by reference.



    Calling a method on an object is very similar to the object being an argument in the call. The recursive call has an object with a smaller width and at one point it will hit the base case. You could say this is the same:



    public static int getArea(Triangle t) 
    if (t.width <= 0)
    return 0;
    elseif (t.width == 1)
    return 1;
    else
    Triangle t2 = new Triangle(t.width - 1);
    int area = getArea(t2);
    return area + t.width;




    Again.. a method being recusive does not change anything. It ahs no special treatment. It needs its calls to finish before it can return the value, just like if you used a different method to get the area in getArea.. No difference at all.






    share|improve this answer




















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236667%2flocal-variable-in-recursion-retaining-values%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote



      accepted










      Each method call details are stored on stack, once the value is returned from the method call the execution returns to the previous method calling the current method, the local variable values,etc,. would be stored on the stack and that's how those values are used in the execution when required by program. Do some experimentation around recursive programs to understand more about it.



      My suggestion would be try debugging recursive programs using break points on IDE's like eclipse or Intellij, it would clear a lot of confusion and brings clarity on how recursion works.






      share|improve this answer


















      • 1




        Thanks for the help hrudayanath, will definitely take a look at it w/ the debugger
        – Matthew
        Nov 10 at 7:10














      up vote
      3
      down vote



      accepted










      Each method call details are stored on stack, once the value is returned from the method call the execution returns to the previous method calling the current method, the local variable values,etc,. would be stored on the stack and that's how those values are used in the execution when required by program. Do some experimentation around recursive programs to understand more about it.



      My suggestion would be try debugging recursive programs using break points on IDE's like eclipse or Intellij, it would clear a lot of confusion and brings clarity on how recursion works.






      share|improve this answer


















      • 1




        Thanks for the help hrudayanath, will definitely take a look at it w/ the debugger
        – Matthew
        Nov 10 at 7:10












      up vote
      3
      down vote



      accepted







      up vote
      3
      down vote



      accepted






      Each method call details are stored on stack, once the value is returned from the method call the execution returns to the previous method calling the current method, the local variable values,etc,. would be stored on the stack and that's how those values are used in the execution when required by program. Do some experimentation around recursive programs to understand more about it.



      My suggestion would be try debugging recursive programs using break points on IDE's like eclipse or Intellij, it would clear a lot of confusion and brings clarity on how recursion works.






      share|improve this answer














      Each method call details are stored on stack, once the value is returned from the method call the execution returns to the previous method calling the current method, the local variable values,etc,. would be stored on the stack and that's how those values are used in the execution when required by program. Do some experimentation around recursive programs to understand more about it.



      My suggestion would be try debugging recursive programs using break points on IDE's like eclipse or Intellij, it would clear a lot of confusion and brings clarity on how recursion works.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 10 at 7:26

























      answered Nov 10 at 7:03









      Hrudayanath

      13310




      13310







      • 1




        Thanks for the help hrudayanath, will definitely take a look at it w/ the debugger
        – Matthew
        Nov 10 at 7:10












      • 1




        Thanks for the help hrudayanath, will definitely take a look at it w/ the debugger
        – Matthew
        Nov 10 at 7:10







      1




      1




      Thanks for the help hrudayanath, will definitely take a look at it w/ the debugger
      – Matthew
      Nov 10 at 7:10




      Thanks for the help hrudayanath, will definitely take a look at it w/ the debugger
      – Matthew
      Nov 10 at 7:10












      up vote
      1
      down vote













      When it comes to recursion, I often find it helpful to remember that what you give is what you get, meaning that what you get when you call your recursive method is whatever you decide to return from it.



      In this case, what you get when you write



      int area = t2.getArea();


      is either 0, 1 or area + width.



      The last case is the recursive case where you recursively define a new Triangle instance with the new width decremented 1 and then call .getArea() on that. That is functionally equivalent to just defining .getArea() as



      protected int getArea(int width) 

      if(width <= 0)
      return 0;
      else if(width == 1)
      return 1;
      else
      int area = getArea(width - 1);
      return area + width;





      It makes no difference whether you call .getArea() from one or the other instance of Triangle. All that matters is what you define width to be when calling it (in this case width - 1) and how you define its influence on the return value.






      share|improve this answer


























        up vote
        1
        down vote













        When it comes to recursion, I often find it helpful to remember that what you give is what you get, meaning that what you get when you call your recursive method is whatever you decide to return from it.



        In this case, what you get when you write



        int area = t2.getArea();


        is either 0, 1 or area + width.



        The last case is the recursive case where you recursively define a new Triangle instance with the new width decremented 1 and then call .getArea() on that. That is functionally equivalent to just defining .getArea() as



        protected int getArea(int width) 

        if(width <= 0)
        return 0;
        else if(width == 1)
        return 1;
        else
        int area = getArea(width - 1);
        return area + width;





        It makes no difference whether you call .getArea() from one or the other instance of Triangle. All that matters is what you define width to be when calling it (in this case width - 1) and how you define its influence on the return value.






        share|improve this answer
























          up vote
          1
          down vote










          up vote
          1
          down vote









          When it comes to recursion, I often find it helpful to remember that what you give is what you get, meaning that what you get when you call your recursive method is whatever you decide to return from it.



          In this case, what you get when you write



          int area = t2.getArea();


          is either 0, 1 or area + width.



          The last case is the recursive case where you recursively define a new Triangle instance with the new width decremented 1 and then call .getArea() on that. That is functionally equivalent to just defining .getArea() as



          protected int getArea(int width) 

          if(width <= 0)
          return 0;
          else if(width == 1)
          return 1;
          else
          int area = getArea(width - 1);
          return area + width;





          It makes no difference whether you call .getArea() from one or the other instance of Triangle. All that matters is what you define width to be when calling it (in this case width - 1) and how you define its influence on the return value.






          share|improve this answer














          When it comes to recursion, I often find it helpful to remember that what you give is what you get, meaning that what you get when you call your recursive method is whatever you decide to return from it.



          In this case, what you get when you write



          int area = t2.getArea();


          is either 0, 1 or area + width.



          The last case is the recursive case where you recursively define a new Triangle instance with the new width decremented 1 and then call .getArea() on that. That is functionally equivalent to just defining .getArea() as



          protected int getArea(int width) 

          if(width <= 0)
          return 0;
          else if(width == 1)
          return 1;
          else
          int area = getArea(width - 1);
          return area + width;





          It makes no difference whether you call .getArea() from one or the other instance of Triangle. All that matters is what you define width to be when calling it (in this case width - 1) and how you define its influence on the return value.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 at 11:29

























          answered Nov 10 at 8:09









          runcoderun

          38917




          38917




















              up vote
              0
              down vote













              I think you are looking at this wrong. If you call two methods. eg.



              public int test() 
              int x = getSomeInt(1);
              int y = getSomeInt(2);
              return x + y;



              Are you ever questioning whether return x + y is done or if the value of x is determined before y? It does it from top to bottom and the statement setting y does not start before getSomeInt(1) has returned and its value set as x.
              So to your example:



              protected int getArea() 
              if (width <= 0)
              return 0;
              elseif (width == 1)
              return 1;
              else
              Triangle t2 = new Triangle(width - 1);
              int area = t2.getArea();
              return area + width;




              So if you have a Triangle with width 1 and call getArea you get 1 back.



              What happens if you do it on a Triangle with width 2? Well it creates t2 with width 1 and call getArea on it. We already know the result since we calculated it already. area becomes 1 and then it returns 1 + 2.



              What happens if you do it with width 3?. It will create t2 with width 2 and call getArea() on that. We know that returns 3 from the above and the result is 3 + 3.



              The recusive method gets called with a high with, but it is the one with 1 that gets determined first, then 2, 3, 4, ...and finally the call you actually called has some area that it adds its with to.



              Each call to a methoid has nothing to do with the callee. It is the same code yes, but it's a different object and the local variables are unique to the call just as the two calls getSomeInt also have two differen versions of whatever it called its first parameter. They are not entangled unless you are mutating or passing by reference.



              Calling a method on an object is very similar to the object being an argument in the call. The recursive call has an object with a smaller width and at one point it will hit the base case. You could say this is the same:



              public static int getArea(Triangle t) 
              if (t.width <= 0)
              return 0;
              elseif (t.width == 1)
              return 1;
              else
              Triangle t2 = new Triangle(t.width - 1);
              int area = getArea(t2);
              return area + t.width;




              Again.. a method being recusive does not change anything. It ahs no special treatment. It needs its calls to finish before it can return the value, just like if you used a different method to get the area in getArea.. No difference at all.






              share|improve this answer
























                up vote
                0
                down vote













                I think you are looking at this wrong. If you call two methods. eg.



                public int test() 
                int x = getSomeInt(1);
                int y = getSomeInt(2);
                return x + y;



                Are you ever questioning whether return x + y is done or if the value of x is determined before y? It does it from top to bottom and the statement setting y does not start before getSomeInt(1) has returned and its value set as x.
                So to your example:



                protected int getArea() 
                if (width <= 0)
                return 0;
                elseif (width == 1)
                return 1;
                else
                Triangle t2 = new Triangle(width - 1);
                int area = t2.getArea();
                return area + width;




                So if you have a Triangle with width 1 and call getArea you get 1 back.



                What happens if you do it on a Triangle with width 2? Well it creates t2 with width 1 and call getArea on it. We already know the result since we calculated it already. area becomes 1 and then it returns 1 + 2.



                What happens if you do it with width 3?. It will create t2 with width 2 and call getArea() on that. We know that returns 3 from the above and the result is 3 + 3.



                The recusive method gets called with a high with, but it is the one with 1 that gets determined first, then 2, 3, 4, ...and finally the call you actually called has some area that it adds its with to.



                Each call to a methoid has nothing to do with the callee. It is the same code yes, but it's a different object and the local variables are unique to the call just as the two calls getSomeInt also have two differen versions of whatever it called its first parameter. They are not entangled unless you are mutating or passing by reference.



                Calling a method on an object is very similar to the object being an argument in the call. The recursive call has an object with a smaller width and at one point it will hit the base case. You could say this is the same:



                public static int getArea(Triangle t) 
                if (t.width <= 0)
                return 0;
                elseif (t.width == 1)
                return 1;
                else
                Triangle t2 = new Triangle(t.width - 1);
                int area = getArea(t2);
                return area + t.width;




                Again.. a method being recusive does not change anything. It ahs no special treatment. It needs its calls to finish before it can return the value, just like if you used a different method to get the area in getArea.. No difference at all.






                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  I think you are looking at this wrong. If you call two methods. eg.



                  public int test() 
                  int x = getSomeInt(1);
                  int y = getSomeInt(2);
                  return x + y;



                  Are you ever questioning whether return x + y is done or if the value of x is determined before y? It does it from top to bottom and the statement setting y does not start before getSomeInt(1) has returned and its value set as x.
                  So to your example:



                  protected int getArea() 
                  if (width <= 0)
                  return 0;
                  elseif (width == 1)
                  return 1;
                  else
                  Triangle t2 = new Triangle(width - 1);
                  int area = t2.getArea();
                  return area + width;




                  So if you have a Triangle with width 1 and call getArea you get 1 back.



                  What happens if you do it on a Triangle with width 2? Well it creates t2 with width 1 and call getArea on it. We already know the result since we calculated it already. area becomes 1 and then it returns 1 + 2.



                  What happens if you do it with width 3?. It will create t2 with width 2 and call getArea() on that. We know that returns 3 from the above and the result is 3 + 3.



                  The recusive method gets called with a high with, but it is the one with 1 that gets determined first, then 2, 3, 4, ...and finally the call you actually called has some area that it adds its with to.



                  Each call to a methoid has nothing to do with the callee. It is the same code yes, but it's a different object and the local variables are unique to the call just as the two calls getSomeInt also have two differen versions of whatever it called its first parameter. They are not entangled unless you are mutating or passing by reference.



                  Calling a method on an object is very similar to the object being an argument in the call. The recursive call has an object with a smaller width and at one point it will hit the base case. You could say this is the same:



                  public static int getArea(Triangle t) 
                  if (t.width <= 0)
                  return 0;
                  elseif (t.width == 1)
                  return 1;
                  else
                  Triangle t2 = new Triangle(t.width - 1);
                  int area = getArea(t2);
                  return area + t.width;




                  Again.. a method being recusive does not change anything. It ahs no special treatment. It needs its calls to finish before it can return the value, just like if you used a different method to get the area in getArea.. No difference at all.






                  share|improve this answer












                  I think you are looking at this wrong. If you call two methods. eg.



                  public int test() 
                  int x = getSomeInt(1);
                  int y = getSomeInt(2);
                  return x + y;



                  Are you ever questioning whether return x + y is done or if the value of x is determined before y? It does it from top to bottom and the statement setting y does not start before getSomeInt(1) has returned and its value set as x.
                  So to your example:



                  protected int getArea() 
                  if (width <= 0)
                  return 0;
                  elseif (width == 1)
                  return 1;
                  else
                  Triangle t2 = new Triangle(width - 1);
                  int area = t2.getArea();
                  return area + width;




                  So if you have a Triangle with width 1 and call getArea you get 1 back.



                  What happens if you do it on a Triangle with width 2? Well it creates t2 with width 1 and call getArea on it. We already know the result since we calculated it already. area becomes 1 and then it returns 1 + 2.



                  What happens if you do it with width 3?. It will create t2 with width 2 and call getArea() on that. We know that returns 3 from the above and the result is 3 + 3.



                  The recusive method gets called with a high with, but it is the one with 1 that gets determined first, then 2, 3, 4, ...and finally the call you actually called has some area that it adds its with to.



                  Each call to a methoid has nothing to do with the callee. It is the same code yes, but it's a different object and the local variables are unique to the call just as the two calls getSomeInt also have two differen versions of whatever it called its first parameter. They are not entangled unless you are mutating or passing by reference.



                  Calling a method on an object is very similar to the object being an argument in the call. The recursive call has an object with a smaller width and at one point it will hit the base case. You could say this is the same:



                  public static int getArea(Triangle t) 
                  if (t.width <= 0)
                  return 0;
                  elseif (t.width == 1)
                  return 1;
                  else
                  Triangle t2 = new Triangle(t.width - 1);
                  int area = getArea(t2);
                  return area + t.width;




                  Again.. a method being recusive does not change anything. It ahs no special treatment. It needs its calls to finish before it can return the value, just like if you used a different method to get the area in getArea.. No difference at all.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 10 at 14:51









                  Sylwester

                  33.7k22854




                  33.7k22854



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236667%2flocal-variable-in-recursion-retaining-values%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Kleinkühnau

                      Makov (Slowakei)

                      Deutsches Schauspielhaus