Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access of private field of another object in copy constructors - Really a problem?

In my Java application I have some copy-constructors like this

public MyClass(MyClass src) {
    this.field1 = src.field1;
    this.field2 = src.field2;
    this.field3 = src.field3;
...
}

Now Netbeans 6.9 warns about this and I wonder what is wrong with this code?

My concerns:

  • Using the getters might introduce unwanted side-effects. The new object might no longer be considered a copy of the original.
  • If it is recommended using the getters, wouldn't it be more consistent if one would use setters for the new instance as well?

EDIT: The actual warning is "Access of private field of another object" and the only available action Netbeans offers is adding a @SuppressWarnings("AccessingNonPublicFieldOfAnotherObject")

My code is actually as trivial as the given example.

like image 982
Daniel Rikowski Avatar asked Mar 12 '10 09:03

Daniel Rikowski


3 Answers

I see no problem with the code. In fact, I share your concerns and would prefer to not use getters.

What exactly is the warning you get? Maybe it is related to something you are not showing us?

Update: Seems Netbeans is really complaining about just that. That is a rather controversial warning to ship with an IDE, I would think.

like image 150
Thilo Avatar answered Nov 13 '22 16:11

Thilo


It reminds me of the discussion ADT vs. Object.

ADT can access the internal state of other instance of the ADT. The object philosophy would prevent that and enforce access through getter/setter to ensure representation independence.

It's been a deliberated choice that instances variable in Java are class-private, not object-private (In the later case only this.privateInstVar is allowed, not obj.privateInstVar).

This has both strength and weaknesses. It's espcially handy when it comes to cloning and equality. On the other hand it can be misused and break encapsulation.

It's almost a philosophical debate. But IMHO, what you are doing is fine.

like image 24
ewernli Avatar answered Nov 13 '22 15:11

ewernli


I don't know why NetBeans warns, but you are effectively making a shallow copy. Your copy shares field1, field2 and field3 with src and as such a modification of field3, say, a List, will reflect in the original.

private List field1;

public MyClass(MyClass src) {
    this.field1 = src.field1;
    this.field2 = src.field2;
    this.field3 = src.field3;

    field1.add(new Object()); // field1 of src also modified
...
}
like image 3
extraneon Avatar answered Nov 13 '22 14:11

extraneon