If I do something like
private int[] myPrivateArray;
and have a getter that returns the array, then the array contents can be modified from outside the class, which sort of defeats the purpose of making it private.
If I do something like
private int[] myPrivateArray;
and have a getter that returns the array, then the array contents can be modified from outside the class, which sort of defeats the purpose of making it private.
Yes, there is.
Firstly, don't provide a getter for that array if you don't have to. If you MUST provide a getter, then pass a copy, not the private variable.
Secondly (which has been overlooked so far), make sure the JVM SecurityManager
is set up to stop reflection.
You can provide setters, getters for the specific index elements of the array, much like what List implementations do. As for returning it from a function, you can copy the array, and pass the copy instead. Rather than passing reference to your array, you pass reference to a copy of your array.
As others have said, you can return a copy of the array:
public int[]getMyArray(){
return myPrivateArray.clone();
}
Alternatively, you can provide a getter to access individual elements of the array:
public int getMyArray(int index){
return myPrivateArray.get(index);
}
Finally, if you'd like to avoid copying but insist on returning all the elements at once, you can return an unmodifiable list view of your array:
public class MyClass{
private Object[]myPrivateArray;
private List<Object>myArrayView=Collections.unmodifiableList(Arrays.asList(myPrivateArray));
public List<Object>getMyArrayView(){
return myArrayView;
}
}
Although this is slightly more complicated with an array of primitives:
public class MyClass{
private int[]myPrivateArray;
private List<Integer>myArrayView=new AbstractList<Integer>(){
@Override public int size(){
return myPrivateArray.length;
}
@Override public Integer get(int index){
return myPrivateArray[index]
}
}
public List<Integer>getMyArrayView(){
return myArrayView;
}
}