In-young Choung February 2016

How to find out where arguments come from to the specific parameter in Java?

I've come to wonder if there is a way to find out which method arguments are returned from.

If you have a few methods in a few classes, perhaps it's easier to figure out where the values come from to the parameter by looking at the method signature.

What if we have very complicated structure of classes? how can we efficiently figure it out? (other than using debugger)

I would appreciate your advice!

For example,

public class Recursion_fig18_9 {

//recursive declaration of method factorial
public static long factorial(long number){
    long result = 1;

    //iterative declaration of method factorial
    for (long i = number; i >=1; i--){
        result *= i;
    }
    return result;
}


//output factorials for values of 0 to 10
public static void main(String[] args) {

    //calculate the factorials from 0 to 10
    for (int counter =0; counter <= 10; counter ++){
        System.out.printf("%d! = %d%n", counter, factorial(counter));
    }
}

(Although this example is quite small structure of class,) How can I figure out where "number" is returned from in the factorial method?

Answers


OYRM February 2016

Using your debugger you can set a break point within the method. When that method breaks, you'll see a stack frame indicating where the call to the method came from and this will help you to trace back the method call to figure out how it was constructed.

Within your code example, place the breakpoint on the return statement so that you can examine the return value before it is returned.

Alternatively, you could use a logger to generate a log file and see how your code steps through it. You'd be able to back off the logging level after the fact so that you don't have to change code constantly. More on logging here

Post Status

Asked in February 2016
Viewed 2,539 times
Voted 13
Answered 1 times

Search




Leave an answer