In Java, Prevent a field from being changed after being returned by instance method
<p>In a software development class at my university, the teacher kept mentioning that on a quiz we needed to make sure that a field returned by a getter needed to be "protected."  I guess she meant that nothing outside the class should be able to change it.  She didn't give much more of an explanation than that.</p>

<p>For instance:</p>

<pre><code>class Foo {
    string[] bar = &lt;some array contents&gt;;

    public string[] getBar() {
        return bar;
    }
}
</code></pre>

<p>Any code calling getBar would be able to modify the elements in that array.  How do you prevent that from happening?  I'm assuming that the object itself should be able to modify the array, just not anything outside the object.</p>

<p>This isn't homework help since the quiz is a couple of weeks old.  I simply want to understand Java better since my teacher didn't explain very well.</p>

<p>Update: The teacher wouldn't merely allow us to use protected as the access modifier on the field.</p>
