0

Say, I have a recursive code as below, where the variable 'resultshere' keeps getting updated in every recursive call. I would like to see the value of this variable from the "previous" call even for the brief period when it's not in current scope, and so does not have a value (yet).

How can I do this in eclipse?

Say my code is something like this:

public class Test {

    public static void main(String[] args) {
        System.out.println(fun(5));
    }

    static int fun(int n) {
        int resultshere = 0;
        int ret = 0;

        if (n == 0) {
            resultshere = 1;
            return resultshere;
        }

        ret = fun(n - 1);
        resultshere = n * ret;

        return resultshere;
    }

}
1

1 Answer 1

2

When debugging in Eclipse you always see the stacktrace. The stacktrace shows the methods that are called one after another. For recursive calls, you will see the same method being called:

Debug View - Stacktrace

Here you see three calls to the fun method. The third call is highlighted, and you see the parameter n being at value 3 (in the variables view).


If you are now interested in the previous calls, simply select the method call you wish:

Debug View - Previous Method

Here you see the first method call selected, and now you see all the variables (and parameters) - that this method call had - in the variables view.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you, this is fantastic, never knew that you could see variables in the previous frame by just clicking on it. I have two questions though. 1.When the call at say n==0 returns some value to the previous call(frame), it's pushed out of the call stack and I can no more see it in the debug view you mentioned. Is it possible to still see these even after return? 2.My debug view is cluttered with calls from my previous debugs maybe, how can I clear these?
If a method call returns, it also disappears from the debug view because it simply does not allocate the stack anymore. This also means that you cannot access the local variables anymore because they simply disappeared, too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.