Using java, is there a way to set certain values in an array to be static/uneditable? I'm trying to make a sudoku game, so I want the initial numbers to be set so the program can't change them, but the other numbers to be changeable. I've done some googling so far but none of my searches have brought up any relevant information.
1 Answer
You have to hide the array by making it private. And never return the reference to the array instead return a clone.
e.g.
public class ArrayHolder {
private String[] array;
public ArrayHolder(String[] inputArray) {
//make a copy of inputArray
//assign the reference to the copy to this.array
}
public String[] getArray() {
//make a copy of the array
//return the reference to the copy
}
}
As far as making some elements updatable, you have to write mutator method(s) in the class, so that only those methods can change certain elements in the array.
finalon the reference to the array, but that will just make it (sort of) impossible to change which array is referenced -- it will not prevent modification of the array.