Java – accessing private fields of another object in the copy constructor – is that really a problem?
In my java application, I have some such copy constructors
public MyClass(MyClass src) { this.field1 = src.field1; this.field2 = src.field2; this.field3 = src.field3; ... }
Now NetBeans 6.9 warns this. I don't know what's wrong with this code?
My doubts
The use of getters may cause unnecessary side effects New objects may no longer be considered copies of the original. > If it is recommended to use getters, it will not be more consistent to use setters for new instances?
Edit: the actual warning is "accessing the private domain of another object". The only available operation provided by NetBeans is to add a @ suppresswarnings ("accessing nonpublicfieldofanotherobject")
My code is actually as trivial as the given example
Solution
I see no problem with the code In fact, I share your concerns rather than use getters
What warning did you get? Maybe it has something to do with what you didn't show us?
Update: it seems that NetBeans really complain about this This is a rather controversial warning, loading an IDE, I'll think