Objects communicate by passing messages (see Section 1.4, p. 7). A message is implemented as a method call to invoke a particular method on an object. Static methods can be invoked on classes in Java. Parameters in the method call provide one way of exchanging information between the caller object and the callee object (which need not be different). Defining methods is discussed in Section 4.3.
The syntax of a method call can be any one of the following:
<object reference>.<method name> (<actual parameter list>)
<class name>.<static method name> (<actual parameter list>)
<method name> (<actual parameter list>)
The <object reference> must be an expression that evaluates to an object reference. If the caller and the callee are the same, then <object reference> can be omitted (see discussion on this reference in Section 4.3 on page 114). The <class name> can be the fully qualified name (see Section 4.6, p. 126) of the class. The <actual parameter list> is comma-separated if there is more than one parameter. The parentheses are mandatory even if the actual parameter list is empty. This distinguishes the method call from the construct for accessing fields, specifying fully qualified names for classes and packages using the dot operator.
objRef.doIt(time, place); // Explicit object reference int i = java.lang.Math.abs(-1); // Fully qualified class name int j = Math.abs(-1); // Class name someMethod(ofValue); // Object or class implicitly implied someObjRef.make().make().make(); // make() returns an object reference
The dot operator . has left associativity. In the last code line, the first call of the make() method returns an object reference that indicates the object to execute the next call, and so on. This is an example of call chaining.
Actual parameters are parameters passed to the method when the method is invoked by a method call, and can vary from call to call. Formal parameters are parameters defined in the method definition (see Section 4.3, p. 112) and are local to the method (see Local variables, p. 33).
Actual and formal parameters must be compatible in the following respects:
The number of actual parameters must equal the number of formal parameters in the method definition.
Corresponding individual actual and formal parameters must be type compatible. Method invocation conversions for primitive values are discussed in Section 3.18, and those for reference types are discussed in Section 6.6.
In Java, all parameters are passed by value, that is, an actual parameter is evaluated and its value is assigned to the corresponding formal parameter. Table 3.20 summarizes what value is passed depending on the type of the formal parameter. In the case of primitive data types, the data value of the actual parameter is passed. If the actual parameter is a reference to an object (i.e., instantiation of a class or an array), then the reference value is passed and not the object itself. If the actual parameter is an array element of a primitive data type, then its data value is passed, and if the array element is a reference to an object, then its reference value is passed.
Data Type of the Formal Parameters
Primitive data types
Primitive data value
It should also be stressed that each invocation of a method has its own copies of the formal parameters, as is the case for any local variable in the method.
The order of evaluation in the actual parameter list is always from left to right. Given the following declaration:
int i = 4;
the method call
is effectively the same as
and not as
For expositional purposes, the examples in subsequent sections primarily show method invocation on the same object or the same class. The parameter passing mechanism is no different when different objects or classes are involved.