Think of the list of dishes like a list of method signatures in a class definition.
Think of a method call as ordering the dish.
Let's see an example of a method call. Suppose you have a class called ShoppingCart, with a method signature that looks like:
// Method signature
int computeCost( int quantity, boolean addTax ) ;
Recall that a method signature appears in a class definition.
(Sort of).
Now we need some place to make a method call. One place is in the main() method of some class. Let's assume we have a class called ShoppingCartTest. This is how it looks:
// Partial class definition public class ShoppingCartTest { // Definition of main() public static void main( String [] args ) { ShoppingCart cart = new ShoppingCart() ; // DO SOMETHING WITH cart } }What does the method signature tell us?
// Method signature for computeCost()
int computeCost( int quantity, boolean addTax ) ;
It says we must provide two arguments. The first argument
must be an int expression, the second must be a boolean
expression.
Here's a simple method call.
// cart.computeCost( 10, true ) is method call
int cost = cart.computeCost( 10, true ) ;
The method call is cart.computeCost( 10, true ).
These are the things you need for a method call.
The first argument is 10, which is an int argument. This matches the type of the first parameter of the parameter list.
The second argument is true, which is a boolean argument. This matches the type of the second parameter of the parameter list.
For example, consider this method signature.
// Method definition with three parameters
int doSomething( int x, double y, boolean z ) ;
This method signature says that an argument list must contain
three arguments. The first argument must be an int expression,
since the first parameter is int x.
The second argument must be an double expression, since the second parameter is double y.
The third argument must be an boolean expression, since the second parameter is boolean z.
The parameter names aren't particularly important in this case. We can use other parameter names, and the parameter list would still have the same requirements on an argument list.
Here's a corresponding method call.
// Method call with three arguments
foo.doSomething( 2 * 3, 3.14 / 2, false ) ;
Notice that the argument list has 3 arguments. The first is
an int expression, the second is a double expression,
and the third is a boolean expression.
Method calls have arguments. Method signatures have parameters.
Method calls cause the command to be sent to the object. Method signatures just tell you what the command must look like when you send it.
Again, let's look at the method signature.
// Method signature with two parameters
int computeCost( int quantity, boolean addTax ) ;
The return type is int, so a corresponding method call
is an int expression.
Let's look at the method call again:
// Method call with two arguments
int cost = cart.computeCost( 10, true ) ;
Let's assume cart.computeCost( 10, true ) evaluates to 100.
When Java runs the code, it evaluates the method call. This
effectively results in the following.
// Method call evaluates to value, 100
int cost = 100 ;
Thus, cost gets initialized to 100, the return value
of the method call.
boolean x = true, y = false ;
// Method call with two arguments
cart.computeCost( 10 + 2, x || y ) ;
The arguments are expressions. When Java runs this code, it
first evaluates the expressions. The first expression evaluates
to 12. The second evaluates to true.
At this point, the method call is:
// Method call after argument evaluation
cart.computeCost( 12, true )
These argument values are then given to the object, cart, which
does some computation, and gives us back a return value. We call
giving arguments to the object, passing arguments to the object.
Of course, we pass both the arguments, as well as the method name,
so the object knows what method is being called on it.
Languages where arguments are evaluated prior to passing to the object are called strict evaluation languages. Some languages wait until the arguments are used inside the object before evaluation. Such languages are often said to perform lazy evaluation (they evaluate only when they must).