By thedarkside ofthemoon


2013-12-09 13:36:16 8 Comments

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?

7 comments

@rachana 2013-12-09 13:41:45

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.

Also see write a Copy Constructor using Deep copy.

@Anugoonj 2013-12-11 11:57:22

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.

@Andrei Nicusan 2013-12-09 13:39:23

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
    }
}

@Andrei Nicusan 2013-12-09 13:49:04

That's correct.

@Cruncher 2013-12-09 13:49:48

@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.

@Suresh Atta 2013-12-09 13:38:40

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());   

      }

@Maroun 2013-12-09 13:39:06

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; 
}

@dcsohl 2013-12-09 13:40:38

That's not default. You have to define it yourself.

@Maroun 2013-12-09 13:41:10

That's what I meant..

@Bathsheba 2013-12-09 13:45:32

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.

@A Paul 2013-12-09 13:41:23

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

@dcsohl 2013-12-09 13:43:18

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.)

@Simeon Visser 2013-12-09 13:38:14

Java does not have a default copy constructor. You'll need to define it yourself.

Related Questions

Sponsored Content

47 Answered Questions

[SOLVED] Does a finally block always get executed in Java?

20 Answered Questions

[SOLVED] Does Java support default parameter values?

21 Answered Questions

[SOLVED] Why should I use a pointer rather than the object itself?

  • 2014-03-03 11:54:16
  • gEdringer
  • 297149 View
  • 1527 Score
  • 21 Answer
  • Tags:   c++ pointers c++11

21 Answered Questions

[SOLVED] How do I call one constructor from another in Java?

  • 2008-11-12 20:10:19
  • ashokgelal
  • 807219 View
  • 2136 Score
  • 21 Answer
  • Tags:   java constructor

13 Answered Questions

[SOLVED] Why does Java have transient fields?

  • 2009-05-26 12:11:36
  • Animesh
  • 668256 View
  • 1400 Score
  • 13 Answer
  • Tags:   java field transient

19 Answered Questions

[SOLVED] Why do this() and super() have to be the first statement in a constructor?

  • 2009-07-22 21:25:15
  • Joe Daley
  • 212256 View
  • 570 Score
  • 19 Answer
  • Tags:   java constructor

8 Answered Questions

[SOLVED] What is The Rule of Three?

26 Answered Questions

[SOLVED] How does the Java 'for each' loop work?

5 Answered Questions

[SOLVED] What is the copy-and-swap idiom?

Sponsored Content