Why doesn’t Java have a copy constructor?

Java does. They’re just not called implicitly like they are in C++ and I suspect that’s your real question.

Firstly, a copy constructor is nothing more than:

public class Blah {
  private int foo;

  public Blah() { } // public no-args constructor
  public Blah(Blah b) { foo = b.foo; }  // copy constructor
}

Now C++ will implicitly call the copy constructor with a statement like this:

Blah b2 = b1;

Cloning/copying in that instance simply makes no sense in Java because all b1 and b2 are references and not value objects like they are in C++. In C++ that statement makes a copy of the object’s state. In Java it simply copies the reference. The object’s state is not copied so implicitly calling the copy constructor makes no sense.

And that’s all there is to it really.

Leave a Comment