3.19 Passing Object Reference Values


If an actual parameter is a reference to an object, then the reference value is passed. This means that both the actual parameter and the formal parameter are aliases to the object denoted by this reference value during the invocation of the method. In particular, this implies that changes made to the object via the formal parameter will be apparent after the call returns. The actual parameter expression must evaluate to an object reference before the reference value can be passed.

Type conversions between actual and formal parameters of reference types are discussed in Section 6.6.

Example 3.4 Passing Object Reference Values
 public class CustomerTwo {     public static void main (String[] args) {         Pizza favoritePizza = new Pizza();          // (1)         System.out.println("Meat on pizza before baking: " + favoritePizza.meat);         bake(favoritePizza);                        // (2)         System.out.println("Meat on pizza after baking: " + favoritePizza.meat);     }     public static void bake(Pizza pizzaToBeBaked) { // (3)         pizzaToBeBaked.meat = "chicken";  // Change the meat on the pizza.         pizzaToBeBaked = null;                      // (4)     } } class Pizza {                                       // (5)     String meat = "beef"; } 

Output from the program:

 Meat on pizza before baking: beef Meat on pizza after baking: chicken 

In Example 3.4, a Pizza object is created at (1). Any object of the class Pizza created using the class definition at (5) always results in a beef pizza. In the call to the bake() method at (2), the value of the object reference in the actual parameter favoritePizza is assigned to the formal parameter pizzaToBeBaked in the definition of the bake() method at (3).

One particular consequence of passing reference values to formal parameters is that any changes made to the object via formal parameters will be reflected back in the calling method when the call returns. In this case, the reference favoritePizza will show that chicken has been substituted for beef on the pizza. Setting the formal parameter pizzaToBeBaked to null at (4) does not change the reference value in the actual parameter favoritePizza . The situation at method invocation, and just before return from method bake() , is illustrated in Figure 3.5.

Figure 3.5. Parameter Passing: Object Reference Values

graphics/03fig05.gif

In summary, the formal parameter can only change the state of the object whose reference value was passed to the method.

The parameter passing strategy in Java is call- by-value and not call-by-reference , regardless of the type of the parameter. Call-by-reference would have allowed values in the actual parameters to be changed via formal parameters; that is, the value in pricePrPizza to be halved in Example 3.3 and favoritePizza to be set to null in Example 3.4. However, this cannot be directly implemented in Java.



A Programmer[ap]s Guide to Java Certification
A Programmer[ap]s Guide to Java Certification
ISBN: 201596148
EAN: N/A
Year: 2003
Pages: 284

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net