Curriculum
Course: Learn Java Programming
Login

Curriculum

Learn Java Programming

Video lesson

Understanding the Copy Constructor in Java

In this lesson, you will learn

  • Copy Constructor in Java
  • Copy Constructor vs Assigning Object Reference
  • Examples

 

What is a Copy Constructor?

  1. A copy constructor is a special constructor in Java used to create a new object by copying the contents of an existing object of the same class.
  2. In Java, a copy constructor is a constructor that takes an object of the same class as a parameter and creates a new object with the same state (i.e., copies the values of the fields from the passed object).

 

Why Use a Copy Constructor?

  • Object Duplication: Easily create exact copies of objects, especially useful for complex objects with many fields.
  • Immutability: Helps create immutable objects (objects whose state cannot be changed after creation).
  • Deep Copies: Enables creating deep copies, where all nested objects are also copied, ensuring independence between the original and the copy.

 

Syntax

public class MyClass {
    // ... fields ...

    // Copy constructor
    public MyClass(MyClass originalObject) {
        // Copy values from originalObject to the new object's fields
        this.field1 = originalObject.field1;
        this.field2 = originalObject.field2;
        // ... copy other fields ...
    }
}

 

Example

Book originalBook=new Book(1984, "George Orwell");
Book copiedBook=new Book(originalBook);

Create an object ‘originalBook’ that duplicates the existing object ‘copiedBook’ passes to it. Let’s understand the workings of a copy constructor.

ParameterizedConstructor

 

Example 1: Book Class

package ch7.l8;

class Book {
    int year;
    String author;
    
    // Regular constructor
    public Book(int y, String a) {
        year = y;
        author = a;
    }
 
    // Copy constructor
    public Book(Book another) {
        year = another.year;
        author = another.author;
    }   
}
 
public class BookCopyMain {
    public static void main(String[] args) {
        Book originalBook = new Book(1984, "George Orwell");
        Book copiedBook = new Book(originalBook);
 
        System.out.println("Original Book: "+originalBook.year 
        		+ ", " + originalBook.author);
        System.out.println("Copied Book: "+copiedBook.year
        		+ ", " + copiedBook.author);
    }
}

Output

Original Book: 1984, George Orwell
Copied Book: 1984, George Orwell

 

Difference between Copy Constructor and Assigning Object Reference

 

Copy Constructor

It’s used to create a new object as a copy of an existing object.

CopyCons2

 

Assigning Object Reference

  1. It copies the reference of an object, not the object itself.
  2. When you use the assignment operator with objects, both references point to the same object in memory, and no new object is created.
  3. If you modify the state of the object through one reference, the changes are visible through the other reference as well, because they refer to the same object.

 

Syntax

//Creates a new object 
Book originalBook= new Book(1984, "George Orwell");
Book refBook= originalBook; 
//refBook now refers to the same object as originalBook

 

CopyCons1

Note:

Use a copy constructor when you need a new object with the same state as an existing object but want to keep the objects independent. Use the assignment operator when you simply need another reference to the same object.

Here’s a breakdown of the difference between a copy constructor and simply assigning object references:

Copy Constructor

  • Creates a new object: A copy constructor creates a brand new object in memory. This new object has its own separate space in memory.
  • Copies data: The copy constructor then copies the data from the original object into this new object. This means the new object has the same values as the original at the time of copying.
  • Independent objects: The original object and the new object created by the copy constructor are completely independent. Changes to one object will not affect the other.

 

Assigning Object Reference

  • Doesn’t create a new object: When you assign an object reference, you’re not creating a new object. Instead, you’re creating a second reference that points to the same object in memory.
  • Shared memory: Both references now point to the same location in memory. There’s only one object.
  • Changes affect both: Because both references point to the same object, any changes made through one reference will be immediately reflected when you access the object through the other reference.

 

Analogy

Think of it like this:

  • Copy Constructor: Imagine you have a document, and you make a photocopy of it. The photocopy is a separate piece of paper. You can write on the photocopy without affecting the original document.
  • Assigning Object Reference: Now imagine you have a document, and you give someone else a link to that document in a shared online folder. You both see the same document. If either of you makes changes, those changes are immediately visible to the other person.

 

Example 2: Rectangle Class

package ch7.l8;

class RectangleCopy {
    double length, width;
 
    public RectangleCopy(double l, double w) {
        length = l;
        width = w;
    }
 
    // Copy constructor
    public RectangleCopy(RectangleCopy anotherRectangle) {
        length = anotherRectangle.length;
        width = anotherRectangle.width;
    }
}
 
public class RectangleCopyMain {
    public static void main(String[] args) {
        RectangleCopy originalRectangle = new RectangleCopy(5.0, 2.0);
        RectangleCopy copiedRectangle = new RectangleCopy(originalRectangle);
        
        System.out.println("Original Rectangle: "+copiedRectangle.length
        		+", "+copiedRectangle.width);
 
        System.out.println("Copied Rectangle: "+copiedRectangle.length
        		+", "+copiedRectangle.width);
    }
}

 

Output

Original Rectangle: 5.0, 2.0
Copied Rectangle: 5.0, 2.0

 


 

End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

5.0
5.0 out of 5 stars (based on 1 review)
Excellent100%
Very good0%
Average0%
Poor0%
Terrible0%

 

 

03/04/2025

Very helpful to understand the concept of copy constructor .

Easy to understand.

Explain the topic with the help of examples.

 

 

Submit a Review