I agree with "If the parameter is part of the class" state ", then use the private member", but this immediately raises a new question: when is the parameter considered as part of the class state?
Just to compliment what I said, I would add:
The parameter is part of the state of the class if, after calling the method, we need to save the value.
Example 1 A parameter is not part of the state of a class.
In the Car class, there is no reason to remember which key was used to start the car.
Class Car{ Lock lock; [...] public boolean startCar (Key keyUsedToStart){ return (canStartCarWithThisKey (keyUsedToStart)); } private boolean canStartCarWithThisKey (Key keyUsedToStart){ return (lock.canStartCarWithThisKey(keyUsedToStart)); } [...] }
Example 2: a parameter is part of the state of a class.
Car class needs to know how much fuel it has.
Class Car{ Fuel fuel; [...] public void putSomeFuel (Fuel fuelToAdd){ this.fuel.add(fuelToAdd); } [...] }
source share