As far as I understand, getters / seters should always make copies to protect data.
However, for many of my classes, it is safe for the recipient to return a reference to the requested property, so the following code
b = a.getB();
b.setC(someValue);
actually changes the state of object a. If I can prove that this is normal for my class, is it good practice to implement getter this way? Should the user be notified of this, for example, in Javadoc? I think this will violate the paradigm hiding the implementation, so I should always assume that state a has not changed and call setter
b = a.getB();
b.setC(someValue);
a.setB(b);
Thanks in advance S
source
share