Making local copies
To review: All argument passing in Java is performed by passing references. That is, when you pass an object, youre really passing only a reference to an object that lives outside the method, so if you perform any modifications with that reference, you modify the outside object. In addition:
- Aliasing happens automatically during argument passing.
- There are no local objects, only local references.
- References have scopes, objects do not.
- Object lifetime is never an issue in Java.
- There is no language support (e.g., const) to prevent objects
from being modified and stop the negative effects of aliasing. You cant
simply use the final keyword in the argument list; that simply prevents
you from rebinding the reference to a different
If youre only reading information from an object and not modifying it, passing a reference is the most efficient form of argument passing. This is nice; the default way of doing things is also the most efficient. However, sometimes its necessary to be able to treat the object as if it were local so that changes you make affect only a local copy and do not modify the outside object. Many programming languages support the ability to automatically make a local copy of the outside object, inside the method. Java does not, but it allows you to produce this effect.