TOPIC 3.6 · STUDY

Methods: Passing and Returning References of an Object

A method parameter of object type receives a copy of the reference, so it aliases the caller's object. Calling a mutator on that parameter changes the shared object. Reassigning the parameter to a new object or to null does not retarget the caller's variable. Returning a reference can hand the caller that same object. private fields remain inaccessible by name from another class.

3.6.A

Passing and returning object references

When a caller passes an object to a method, Java copies the reference value into the parameter. The parameter does not get a second copy of the object's fields. It refers to the same object the caller already named. That shared naming is an alias: two variables, one object.

Because the parameter aliases the caller's object, a method call on that parameter can change the object the caller still holds. In the example below, tighten receives a copy of the reference stored in dial. Calling drop on that reference reduces pressure on the one Gauge. After the call, dial.getPressure() is 7.

public class Gauge {
    private int pressure;
    public Gauge(int p) {
        pressure = p;
    }
    public void drop(int n) {
        pressure -= n;
    }
    public int getPressure() {
        return pressure;
    }
}
public class Workshop {
    public void tighten(Gauge g) {
        g.drop(3);
    }
}

Gauge dial = new Gauge(10);
Workshop shop = new Workshop();
shop.tighten(dial);
System.out.print(dial.getPressure());  // 7

Reassigning the parameter is different. Writing g = new Gauge(0) or g = null only changes which object that local parameter names. The caller's variable still refers to the original object. The snippet below leaves dial at pressure 10 even though the method builds a new Gauge and points g at it.

public class Workshop {
    public void replace(Gauge g) {
        g = new Gauge(0);
        // g = null; would also leave the caller unchanged
    }
}

Gauge dial = new Gauge(10);
Workshop shop = new Workshop();
shop.replace(dial);
System.out.print(dial.getPressure());  // still 10
Two reference boxes labeled caller and parameter both point with arrows to one shared object box.

The parameter stores a copy of the caller's reference, so both names point at one object until the parameter is reassigned.

A method can also return a reference. Returning the parameter hands the caller that same object, not a duplicate. If the caller stores the result in a second variable, the two names alias. Later mutations through either name are visible through both.

Aliasing does not unlock private fields. Another class may hold a Gauge reference and call public methods such as drop or getPressure, but it cannot write g.pressure by name. Code inside Gauge may read another Gauge parameter's private fields because that parameter has the enclosing class type.

Key points

  • An object parameter receives a copy of the reference and aliases the caller's object.
  • Calling a method on that parameter can change the caller's object.
  • Reassigning the parameter to a new object or to null does not change the caller's variable.
  • Returning a reference can give the caller the same object, creating another alias.
  • private fields stay inaccessible by name from a different class.

Reassigning the parameter does not retarget the caller's variable. Mutating through the alias changes the shared object; pointing the parameter elsewhere only updates that local copy of the reference.

Practice this topic