I am using ArrayList in one of my Java project classes. The class keeps track of whether the list has been modified and offers several public methods for adding and removing items from the list that automatically set the variable to “changed”.
So far, the list is open, because I want my list to be publicly available to everyone. But I want the class to which the list belongs to be able to modify it. Therefore, no changes from the outside classes. Is it possible? If so, how?
Typically, you probably use the getter and setter methods to control access. But even with the getter method and the list set for another private class, one could still do it getList().remove(element)and thereby change the list from the outside without the class noticing that the list was changed.
CGFoX source
share