-3

Why aren't variables I modify inside myMethod are not modified outside that scope?

public class TestLoop {

    public static void main(String[] args) {
        myMethod(0, 3);

    }

    static void myMethod(int i, int j) {
        System.out.println("i for:" + i + " j:" + j);
        if (i == j)
            return;
        else {

            myMethod(i + 1, j);
            System.out.println("after myMethod Call for:  i:" + i + " j: " + j);
        }
        System.out.println("outside i for: " + i);
    }
}
clearlight
  • 12,255
  • 11
  • 57
  • 75
Rose K.
  • 9
  • 2
  • 3
    What do you mean "rolled back"? The variables don't exist anywhere else than in the method, how can they be anything after return? If you mean that the call to the method doesn't change the variables outside the method then that's as it is. It has nothing to do with `return`, variables just don't change outside. How would `0` or `3` change? – Sami Kuhmonen Jan 03 '17 at 07:20
  • just a recursive call and terminates when local variable satisfy condition `i==j` , read about local variables and return statement – Pavneet_Singh Jan 03 '17 at 07:21
  • you can `return;` in a void function, which kind of acts like a `break` inside of a loop – SomeJavaGuy Jan 03 '17 at 07:21
  • See http://stackoverflow.com/questions/744676/what-does-the-return-keyword-do-in-a-void-method-in-java – GhostCat Jan 03 '17 at 07:23
  • 1
    Your code seems to assume that calling `myMethod` can change the value of its variables. It can't. Java is strictly pass by value. See http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value If you want a value back from a method you call then you need to return that value. – sprinter Jan 03 '17 at 07:25
  • Duplicate of: http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value/40523#40523 – clearlight Jan 05 '17 at 16:44

2 Answers2

1

Each time you call myMethod(i + 1, j), new local variables i and j are created on the stack and initialized to the values passed to them by the caller.

When each execution of myMethod() returns, the local variables i and j get out of scope. You return to the previous myMethod() execution, which has its own local variables i and j having their own values.

Finally, when the call stack returns to the original myMethod(0,3) call, that execution has local variables i and j with values 0 and 3.

The values of i and j never change. There are just multiple local variables named i and j, each one having a limited scope.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • exactly same is happening as you mentioned but never stumble upon such thing. Would be great if refer somewhere more serious example. I saw this piece of code in a merge sort implementation. – Rose K. Jan 05 '17 at 03:47
  • @RoseK, you just didn't know the terminology for that, so you didn't have an easy way to search for it. This has been discussed on Stack Overflow and all over the Internet, so now you have a way to get more information, starting with this question: http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value/40523#40523 – clearlight Jan 05 '17 at 16:47
0

The JLS - Chapter 14. Blocks and Statements contains the answer for your question:

The break (§14.15), continue (§14.16), and return (§14.17) statements cause a transfer of control that may prevent normal completion of statements that contain them.

The control is returned back to the statement where the method was called from.

Maroun
  • 94,125
  • 30
  • 188
  • 241