Java Reference

In-Depth Information

You can see some differences we have to make in our original code for Chocol-

ate_computation method. We are no longer doing multiplication of a and b inside this meth-

od. Instead this multiplication we are doing in the new method Pass_chocolate. We are

calling Pass_chocolate method inside our Chocolate_computation to do the multiplication.

We have also shown that a method can be called inside another method using parameter

passing. Parameters are the variables which you call inside a method which are defined

outside.

Even without parameter passing, we can call methods inside another method and this meth-

od will do some computation. This method has no parameters and there is some return type.

Let us see what happens when we have this type of method.

Public class my_class{

int x (){

A = 10

B = 5

Y = A + B

Return Y

}

}

Analysis
:

This method is doing computation of adding two variables and then returning the value of

this calculation. If this method is good? No it is not. Why? It is doing calculation for two

variables but you can not change value of these variables from outside. Any methods cre-

ated in your program must interact with other methods. This is a big design issue. We will

learn about these aspects later.

Figure 1.8: Method call

You can use a method call from one method to another in cases when you need to do some

computation separately. In such cases it is better to create a separate method you do this