This question is about copy constructors: let’s say that you are given the following two accessor methods that belong to a class called SomeClass. An accessor method is simply a method used to get access to the instance variables of a class:

 
public class SomeClass
{
	public String getName()
	{
			return lastName;
	}

	public Year getYear()
	{
			return new Year(leapYear);
	}
}

 

You can assume that leapYear is a private instance variable of the Year class, and lastName is a private instance variable of the String class. Why is it that a copy constructor is being called in the second method, but not in the first? Why even use a copy constructor in the first place?




Before we explain this problem further, it helps to define a copy constructor: A copy constructor is a constructor that takes only one parameter which is the same exact type as the class in which the copy constructor is defined. For example, suppose we define a class called X. Then, a copy constructor defined in class X would also expect a parameter of type X. In the example above, the statement "return new Year(leapYear);" is using a copy constructor because Year is a class and leapYear is an object of the Year class.

A copy constructor is used to create another object that is a copy of the object that it takes as a parameter. But, the newly created copy is actually independent of the original object. It is independent in the sense that the copy is located at a completely different address in memory than the original.

Why copy constructors are used

So, why is it that the second method (getYear) is using a copy constructor? Let’s say we redefined getYear, so that it doesn’t use a copy constructor. It would look like this:

//bad version of getYear, no copy constructor
public Year getYear()
{
	return leapYear;
}

The problem here is that a reference to the private instance variable ‘leapYear’ is being returned. This means that anyone could declare a SomeClass object, call the accessor method getYear() with that object and assign the return value to a Year object. The danger with this is that the Year object now holds a reference to what was supposed to be a private instance variable (leapYear), thereby violating its privacy. This is shown below:

/* let's say that we're using the bad definition of getYear,
   without a copy constructor  */
public class SomeClass
{
	public Year getYear()
	{
		return leapYear;
	}
}

//suppose the following is done in some other class:
SomeClass someObj = new SomeClass();

Year badObj = someObj.getYear();  

//badObj now has a reference to leapYear, which 
//it shouldn't be able to access.

//With a reference, badObj can now change the 
//contents of the private instance variable leapYear




By using a copy constructor in the definition of getYear(), a separate Year object is created entirely. This object has the same values stored in the instance variables as the original leapYear object, but with its own address space. This way the original leapYear is safe.

In the getName() method defined above, it looks like we will encounter the same problem, because we are returning a reference to a private instance variable. So now the question is why isn’t a copy constructor being used in the method getName()?

The answer is that it simply does not need a copy constructor because the value of the instance variable "lastName" can not be changed, even if we have a reference to it. This is because the String class is what’s called an immutable class – which means that it contains no mutator methods that would allow us to change any String object. So, even with the reference, we can’t modify "lastName". This is why it’s perfectly safe to return a reference to "lastName" even though it’s a private instance variable.

Hiring? Job Hunting? Post a JOB or your RESUME on our JOB BOARD >>

Subscribe to our newsletter for more free interview questions.

  • Gopinath M B

    I think clone also serve the same purpose right?

  • balaji

    copy constructor is much like views in SQL.
    say object 2 is in need of values from object 1.
    If u pass reference of object1 to object2, then object 2 accesses all the members of object1 including object1’s private members.
    This is a security violation.
    To avoid this only a copy of object1’s value is passed to object 2 through copy constructor.
    In other words,
    Object1 and Object2 share same values(data) but they cannot access each others address location(memory).

  • arhdna

    Use copy constructor when ever you want to create shallow copy and deep copy.

  • Ani

    Couldn’t understand well. Where should we use copy constructor? Need more explanation with simple examples.

  • Vinodhini Purushothaman

    Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.

    For example, the Point class was written like this

    public class Point {
    public int x = 0;
    public int y = 0;

    //constructor
    public Point(int a, int b) {
    x = a;
    y = b;
    }
    }

    but it could have been written like this:

    public class Point {
    public int x = 0;
    public int y = 0;

    //constructor
    public Point(int x, int y) {
    this.x = x;
    this.y = y;
    }
    }

    VINODHINI, INDIUM SOFTWARE, AUTOMATION COE

    http://www.indiumsoft.com/core-qa-offerings/test-automation

  • Sudhir Kaushik

    this keywoed in java with all way how it is used..pls explaiin and how they work pls explain line to lline