Mastering advanced encapsulation
The simple maxim of “private data, public methods” (where the public
methods manipulate the data) goes a long way to ensuring proper encapsulation of your data. However, you are not completely safe just yet. In this section, we will review Java’s call by value principle, which is used when passing arguments to and returning values from methods. We will examine how this can present a subtle issue. Lastly, we will examine how to protect your code from encountering this issue in the first place.
Call By value revisited
In Chapter 7, we discussed how, when passing arguments to methods, Java’s call by value mechanism creates copies of those arguments. We saw the need to be aware that when the argument is a reference, such as to an array, the called method can now manipulate the array object that the caller method is looking at.
Similarly, when a method is returning something, call by value applies again. In other words...