Skip to content
APPCITORThe Tech Blog
Esc

Part 14 – Java Methods (Method-call stack)

July 30, 2020 2 min read

Part 14 – Java Methods (Method-call stack)

Hi guys, welcome back. In the last article we discussed about the types of java methods. you can go to the link given below to take look at on the last article.

Today we are going to learn about the method- call stack under the topic java methods.

Let’s identify the method-call stack by step by step with the examples.

Step I

Here according to the order of the commands given in the main method, we should add the values to the method-call stack.

According to the increment command given in the main method, value of x will be replace to 101. So the output will become as,

100

101

Step II

If we have to call a method inside the body of main method, First we should run the “myMethod()” and then call it in the main method. After calling it in the main method, method will erase from the stack and run the next command of the main method.

First value of (100+200) will print in the console. Then after we call the method, value of (10+20) will print in the console. Next the method will erase from the stack and will print the value (100+200) again. The output will be,

300

30

300

Step III

If the method is a parameterized method and when we call the method in the main method,the compiler will take the values from the stack and assign them to the parameters. Then the method body will run.

The values of x and y are 10 and 20 as given in the main method. So they will assign to the parameters of the method when we call the method. But because of the increments of x and y, the values will replace to 11 and 21 and will assign them again to the parameters as the method is calling again in the main method. The output will be,

Total : 30

Total : 32

Step IV

Values for the z of the main method will assign from the return value of the method “getTotal()”

Output will be,

Total : 30

Hope you guys understood about the method-call stack. Let’s meet from the next article on the topic “Rules for Methods” as the next part of the Java Methods.

Thank you.

Share this article

Keep reading