Does Java has a default copy constructor as C++? If it has one - does it remain usable if I declare another constructor (not a copy constructor) explicitly?

- 118,144
- 57
- 340
- 684

- 2,251
- 6
- 31
- 48
-
By a copy constructor, do you mean a constructor that accepts an object of the same type, and duplicates it? If yes - then no, java doesn't have that. – Cruncher Dec 09 '13 at 13:36
-
No it doesn't have a default copy constructor. – Dec 09 '13 at 13:37
-
2http://stackoverflow.com/questions/827785/why-doesnt-java-have-a-copy-constructor – Cruncher Dec 09 '13 at 13:39
7 Answers
Java does not have bulit-in copy constructors.
But you can write your own such constructors. See an example below:
class C{
private String field;
private int anotherField;
private D d;
public C(){}
public C(C other){
this.field = other.field;
this.anotherField = other.anotherField;
this.d = new D(other.d); //watch out when copying mutable objects; they should provide copy constructors, as well. Otherwise, a deep copy may not be possible
}
//getters and setters
}
class D{//mutable class
//fields
public D(D other){
//this is a copy constructor, like the one for C class
}
}

- 4,555
- 1
- 23
- 36
-
2@RomanVottner To expand, this is not always simple, especially if you cannot change one of the fields that are mutable to give it a copy constructor. – Cruncher Dec 09 '13 at 13:49
Java does not have a default copy constructor. You'll need to define it yourself.

- 118,920
- 18
- 185
- 180
No, it doesn't have a default copy constructor. A default constructor.
You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument constructor of the superclass. In this situation, the compiler will complain if the superclass doesn't have a no-argument constructor so you must verify that it does. If your class has no explicit superclass, then it has an implicit superclass of Object, which does have a no-argument constructor.
Usually I provide a one like,
public class CopyConEx {
/**
* Regular constructor.
*/
public CopyConEx(type field1, type field2) {
this.field1 = field1;
this.field2 = field2;
}
/**
* Copy constructor.
*/
public CopyConEx(CopyConEx aCopyConEx) {
this(aCopyConEx.getField1(), aCopyConEx.getField2());
}

- 120,458
- 37
- 198
- 307
There is a copy constructor (But not default one), but it should be called explicitly (In C++ it'll be implicitly called when needed):
public MyClass(MyClass toCopy) {
someField = toCopy.someField;
}

- 94,125
- 30
- 188
- 241
Java support cloning but not using copy constructor. Please find below url on Java cloning.
http://adtmag.com/articles/2000/01/18/effective-javaeffective-cloning.aspx

- 8,113
- 3
- 31
- 61
-
If you read any of the other answers, you'll find that of course Java supports copy constructors. They're just not there by *default*, any more than `clone()` is supported by default (you have to implement `Cloneable` and define `clone()` yourself). (And your article is nearly 14 years old... not sure I'd still trust something written in the days of Java 1.2.) – dcsohl Dec 09 '13 at 13:43
In Java the provision of an automatic copy constructor would be pointless.
Java doesn't need one since you can only have references to objects. (In C++ you can pass objects round by value so the grammar needs to allow for an automatic object copy).
If you need to take deep copies of object in Java, then implement Cloneable
.

- 231,907
- 34
- 361
- 483
Like C++, Java also supports copy constructor. But, unlike C++, Java doesn’t create a default copy constructor if you don’t write your own.

- 3,344
- 7
- 30
- 49
-
just to add java doesn't create a default copy coz there isn't much need for it in normal situations. It's only pass by reference in java. – Anugoonj Dec 11 '13 at 11:57