---
title: "Creating a Copy Constructor"  
description: "One more benefit of constructor overloading is the ability to create a copy constructor, which allows us to create an object copy from the values of a"  
author: "zack mathews"  
published: 2016-05-19  
updated: 2018-03-15  
canonical: https://www.mindstick.com/blog/11103/creating-a-copy-constructor  
category: "java"  
tags: ["java", "class"]  
reading_time: 4 minutes  

---

# Creating a Copy Constructor

One more benefit of constructor overloading is the ability to create a copy **constructor**, which allows us to create an object copy from the values of another similar object. This gives us two [independent](https://answers.mindstick.com/qa/98599/what-is-the-longest-river-in-the-commonwealth-of-independent-states) objects to work with, and changes made to one will not affect the other. The example shown here explains how to write a [copy constructor](https://www.mindstick.com/blog/170/copy-constructor-in-c-sharp).\

```
public class TimeOfDay {
                     private int hour, mins;                     public TimeOfDay(int hour, int mins) {                             this.hour = hour;                             this.mins = mins;
                     }
                     public TimeOfDay(TimeOfDay other) {                             this(other.hour, other.mins);                     }
                     public String toString() {                             return (this.hour + ":" + this.mins);                     }                     public static void main(String[] args) {                             TimeOfDay currentTime = new TimeOfDay(12, 30);                             TimeOfDay copyOfCurrentTime = new TimeOfDay(currentTime);
                             System.out.println("Original: " + currentTime.toString());
                             System.out.println("Copy: " + copyOfCurrentTime.toString());
                     }}
```

\

The class **TimeOfDay** defines two constructors—one that takes two arguments of the integer type and one that takes an [object reference](https://www.mindstick.com/forum/376/system-nullreferenceexception-object-reference-not-set-to-an-instance-of-an-object) of the same type. The first constructor accepts the values of hour and mins from the user and initializes the created object’s state with these values.

The second constructor calls this **two-argument constructor** with the help of the this keyword, passing the state of the object that is passed to it as a parameter. The main program creates two objects using these two constructors and prints their state on the user’s console to verify that both have the same state.

To print the state of the TimeOfDay object, we override the [toString method](https://www.mindstick.com/forum/160331/what-is-the-tostring-method-in-javascript-and-how-can-it-be-used-for-type-conversion) of the [Object class](https://www.mindstick.com/articles/11908/object-class-in-java). Remember that every class in Java inherits from Object, which is a top-level class. In the overridden toString method, we format a string containing the state values of the current object and return it to the caller.

The default [implementation](https://www.mindstick.com/forum/33764/how-to-implementation-of-class-in-c-sharp) of the toString [method returns](https://www.mindstick.com/forum/12904/main-method-returns-if-submethod-has-stoped-by-return-in-c-sharp) an object reference. Many times, developers override this method to print the object’s state in the desired format.

##### Invoking Constructor Overview:

So far, we have overloaded constructors to provide [default values](https://www.mindstick.com/blog/10881/default-values) to some of the fields of a class, to provide a different [initialization](https://www.mindstick.com/interview/160/what-is-the-difference-between-assignment-and-initialization), and to create a copy constructor. Here are the key points to remember:

·To invoke a parent constructor, we must place a call to super in the first line of the constructor. Calling super after the first statement results in an error.

·The parameters we pass to super decide which specific implementation of the parent class constructor is called.

·The first statement in the constructor may be a call to super or this. We use super to call the superclass constructor and we use this to call some other constructor defined within the same class.

·If no this or super call is used in a constructor, the compiler adds an implicit call to super(), which calls the parent “no-arguments” constructor. The no-arguments constructor may be implicit or explicitly declared. If no such constructor is available in the parent constructor, the compiler generates an error.

·The compiler does not provide a no-arguments [default constructor](https://www.mindstick.com/forum/34531/default-constructor) if we write our own constructor (either a no-arguments constructor or a constructor with arguments).

---

Original Source: https://www.mindstick.com/blog/11103/creating-a-copy-constructor

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
