I'm new to Java and I wonder why it is possible to set the value of a private attribute this way, without using a setter ? :
class Example {
private int[] thing;
public void initialize() {
thing = new int[10];
}
public int[] getThing() {
return thing;
}
}
class myclass {
public static void main(String args[]) {
Example e = new Example();
e.initialize();
e.getThing()[0] = 1;
System.out.println(e.getThing()[0]) // value is still 1...
}
I really do not understand why this is legal...
Edit: I expected e.getThing() to return the value of thing not a reference to thing and even if it was the case, as "thing" is private I thought I wont be able to modify it directly.