Sending Parameters to a Method

Purpose:

To call a method with paramters.

Mechanics:

<instance name>.<method name>(<actual parameter list>);

where <actual parameter list> is a space-separated list of variables, constants, or expressions.

Example:

int size = 12;
pen.drawCircle(size);

or

pen.drawCircle(12);

Usage:

  • The actual parameters sent by this statement must have the same data types as the formal parameters declared in the corresponding method declaration. That is, if the first parameter received by a method is an int, then <parameter1> must also be an int.
  • The parameters are separated by commas. The value of the first parameter listed in the method call is received by the first parameter listed in the method declaration, and so on, with a one-to-one correspondence. This implies that there must be the same number and type of actual and formal parameters.
  • If the sending method expects a base type, the corresponding actual parameter may be a variable, constant, or expression. If the method expects a reference to an instance, the actual parameter may be a variable or the result of a function call (including a call to the function new).
Back to the Table of Contents
email suggestions to: cs015tas@cs.brown.edu