Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need copy constructor and when should we use copy constructor in java

I was going through Copy Constructors, I have gone through the links in stack over flow and others as well. But i am not clear on the following points.

  1. Why do we need a Copy Constructor
  2. When do we need a Copy Constructor

I mean what is the exact situation or scenario we would need to use Copy Constructor. Can some one explain with an example or point out links so that i can go through and understand them in clear.

Following are the links i have gone through to get an understanding of what is a copy constructor.

http://www.programmerinterview.com/index.php/java-questions/how-copy-constructors-work/

https://deepeshdarshan.wordpress.com/2013/12/05/copy-constructors-in-java/

The second link explains 'why' and 'where' the copy constructor is to be used. But still i am not clear on it.

Below is my class Employee.java

package com.test;  /**  * @author avinashd  *  */ public class Employee {      private String rollNo;     private String name;      //constructor     public Employee(String rollNo, String name){          this.rollNo = rollNo;         this.name = name;     }      //copy constructor     public Employee(Employee employee){      this.rollNo = employee.rollNo;     this.name = employee.name;      }      public String getRollNo() {         return rollNo;     }      public void setRollNo(String rollNo) {         this.rollNo = rollNo;     }      public String getName() {         return name;     }      public void setName(String name) {         this.name = name;     } } 

Copy Constructor is used to create and exact copy of an object with the same values of an existing object.

Say for example we have an Employee with values as rollNo: 1 and name: avinash. Copy Constructor would create a similar object with values as rollNo: 1 and name: avinash . But both are 2 different objects and changes to the values of on object will not affect another object.

The Question here is

When we have a constructor such as

public Employee(String rollNo, String name){     this.rollNo = rollNo;     this.name = name; } 

to create an object. We can call the same constructor to create another object. But why do we need to call copy constructor.When do we need to call it ?. Please explain

like image 626
Avinash Reddy Avatar asked Mar 31 '15 06:03

Avinash Reddy


People also ask

Why do we need a copy constructor?

Copy constructor is used to initialize the members of a newly created object by copying the members of an already existing object. Copy constructor takes a reference to an object of the same class as an argument.

Why and when do you need copy constructors Is there any situation where you want to only implement a copy constructor or an assignment operator but not both?

Copy Constructor is called when an object is either passed by value, returned by value, or explicitly copied. If there is no copy constructor, c++ creates a default copy constructor which makes a shallow copy. If the object has no pointers to dynamically allocated memory then shallow copy will do.


1 Answers

There are 2 good reasons for using a copy constructor instead of the constructor passing all parameters :

  1. when you have a complex object with many attributes it is much simpler to use the copy constructor
  2. if you add an attribute to your class, you just change the copy constructor to take this new attribute into account instead of changing every occurence of the other constructor
like image 113
Serge Ballesta Avatar answered Oct 11 '22 11:10

Serge Ballesta