8

How do I build a copy constructor that receive another point (x,y) and copy its values ?

I decide a signature: public Point1 (Point1 other) , but I don't know what to write in it...

The Point class looks like:

public class Point1

{
    private int _x ,  _y;    
    public Point1 (Point1 other)
    {
        ...
        ...
    }
//other more constructors here...

}

I tried:

public Point1 (Point1 other)
{
    _x = other._x ;
    _y = other._y;
}

But I almost sure I can do it better..

thnx

Master C
  • 1,536
  • 4
  • 14
  • 19
  • @Master C, I think you mean to have `public Point1(Point1 other)` in your examples, no? – Edwin Buck Apr 21 '11 at 20:07
  • 2
    A `Point` is an example of something that often should be immutable, in which case actually copying one wouldn't be necessary. – ColinD Apr 21 '11 at 20:07
  • 1
    Why do you think it should be some other way? All a copy constructor is supposed to do is copy the values of the other object's fields. – uckelman Apr 21 '11 at 20:08
  • @ColinD, there's plenty of examples where immutable objects can have duplicates. Mutability has to do with ability to change the data, it's not a guarantee of uniqueness of the containing Instance. `String a = new String("a"); String a2 = new String("a");` – Edwin Buck Apr 21 '11 at 20:22
  • 1
    @Edwin: I'm not saying that immutability implies anything about uniqueness of instances, just that there's no point in creating a copy of an immutable object. What good does intentionally creating 2 distinct objects for "a" do? – ColinD Apr 21 '11 at 20:40
  • @ColinD, Well, we might create the other based on standard input, instead of taking the performance hit to check to see if it's already loaded (via a technique similar to String.intern). In addition, code that makes heavy use of multiple class loaders can dispose of duplicates safely if they exist in different class loaders (and the design prevents escape from the class loader). Tomcat does a few items in manners similar to this technique. – Edwin Buck Apr 21 '11 at 20:50
  • 2
    @Edwin: I think you still aren't understanding me. I'm not saying that `String`s must always be interned (or something like it) because having two distinct `String`s with the same contents is bad. It's not. I'm saying that if you already have a `String`, deliberately creating another `String` object with the same contents (using the copy constructor) is pointless. Reading another `String` with the same contents from a `byte[]` or whatever is fine. – ColinD Apr 21 '11 at 21:02
  • 1
    @Edwin I'd argue that String should not have the `String(String)` constructor for the same reasons that ColinD has pointed out. – Steve Kuo Apr 22 '11 at 03:27

2 Answers2

17

Nope, your attempt of

public Point1(Point1 other)
{
    _x = other._x ;
    _y = other._y;
}

is absolutely fine... (I've corrected the parameter type.)

I'd be tempted to make _x and _y final, and make the class final, but that's because I like immutable types. Others definitely have different opinions :)

Cloning on an inheritance hierarchy is slightly trickier - each class in the hierarchy has to have a relevant constructor, pass whatever argument it's given to the superclass constructor, and then copy just its own fields. For example:

public class Point2 extends Point1    
{
    private int _z;
    public Point2(Point2 other)
    {
        super(other);
        this._z = other._z;
    }
}

That's not too bad on the implementation side, but if you want to faithfully clone a Point2 you need to know it's a Point2 in order to call the right constructor.

Implementing Cloneable allows this to be done a bit more simply, but there are other things to consider around that... basically cloning objects isn't as simple as it might appear :) (I'm sure there's an entry in Effective Java for it. If you don't have a copy, buy one now.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

Although the question is quite old but here is an example of copy constructor

public class CopyConstructor {

private int id;
private String name;

public CopyConstructor(int id, String name) {
    super();
    this.id = id;
    this.name = name;
}

public CopyConstructor(CopyConstructor copy) {
    id = copy.id;
    name = copy.name;
}

}

  • Copy constructor is much easier to implement and we don't need to implement Clonable interface.
  • The clone method returns object which needs to cast , we don't need to cast copy constructor.
subhashis
  • 4,629
  • 8
  • 37
  • 52