0

For example, should I use:

public Line(Vector dorigin, Vector ddir) 
{
    origin = dorigin.clone(); 
    dir = ddir.clone();
} 

instead of:

public Line(Vector dorigin, Vector ddir) 
{
     origin = dorigin; 
     dir = ddir;
}

???

So, suppose I have a program like this: Line[] line = new Line[10];

for (i = 0; i < n; i++)
{
    Vector temp = new Vector(i, 0);
    line[i] = new Line(temp, temp);
}
//and then operate on the array line

then I should use the first constructor?

You Peijie
  • 11
  • 1

6 Answers6

2

clone() is often regarded as broken, and I would rather code the constructors explicitly (or delegate to a construction method, but then you can't make use of final to enforce immutability so easily).

To address your edited question, does your constructor need copies of the passed entities, rather than simply references to the original parameters ? If you're passing a collection in, you may need to take a copy of the collection and possibly the entities contained within that collection (that may or may not be possible depending on that entity's implementation). Of course, if your original entities are immutable then it doesn't matter and you can pass references with impunity. You may still worry about copying the collection, however,such that an external party doesn't change that collection (re-orders, adds, removes etc.)

Who actually owns that data ? is a key question here.

Community
  • 1
  • 1
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
0

If the origin is an instance of Line class you should use

public Line(Vector dorigin, Vector ddir) {origin = dorigin; dir = ddir;}
Pratik Shelar
  • 3,154
  • 7
  • 31
  • 51
0

Not necessary clone is not going to call.SO its not needed here. check this http://www.tutorialspoint.com/java/lang/object_clone.htm

anbu selvan
  • 725
  • 3
  • 13
  • 41
0

It depends of what you are going to achieve. If you would like to have a copy of vectors inside the Line or just reference to them.

Probably Vector is immutable so you can easily have just a reference to them. It would be easier and faster.

Also note the Brian's answer about clone() method.

kokosing
  • 5,251
  • 5
  • 37
  • 50
0

IMO is preferable to call clone() in Line construction - not inside the contructor - in this manner

Line l = new Line(dorigin.clone(), ddir.clone());

is more clear and doesn't hide "by value" or "by ref" in contructor, but let caller decide.
This considerations beside all about using (or not using) clone (see @Brian answer)

Luca Basso Ricci
  • 17,829
  • 2
  • 47
  • 69
0

Not sure what you are looking for(Standard practice, efficiency etc.) As far as efficiency goes, the later approach of not cloning is better as it will avoid creating additional copies of the existing object. But it all depends on what you want from your code.

If the requirement is that the objects dorigin and ddir should not be affected in case these objects are modified in Line class, then cloning is the option and you should explore it more.

Else if there is no such requirement, then you better off using cloning.

Santosh
  • 17,667
  • 4
  • 54
  • 79