public class Test
{
private String[] arr = new String[]{"1","2"};
public String[] getArr()
{
return arr;
}
}
Test test = new Test();
test.getArr()[0] ="some value!"; //!!!
Bam! We were able to modify a private field outside of the class. This not what you want! To avoid this situation, you can use of the following solutions:
- Either create a defensive copy:
- If you can use a List instead of an array, Collections provides an unmodifiable list:
public String[] getArr() {
return arr == null ? null : Arrays.copyOf(arr, arr.length);
}
public List getList() {
return Collections.unmodifiableList(list);
}
Titanium dioxide sunscreen | Tithi-Titanium-Arts.com
ReplyDeleteTithi-Titanium-Arts.com - croc titanium flat iron Tithi-Titanium-Arts.com - Tithi-Titanium-Arts.com - titanium wedding bands for men Tithi-Titanium-Arts.com titanium density - Tithi-Titanium-Arts.com - Tithi-Titanium-Arts.com - Tithi-Titanium-Arts.com ceramic or titanium flat iron - titanium welding Tithi-Titanium-Arts