In our previous discussion, we have come to know how to pass an object reference to a method. In the same way, we can pass the reference to a constructor to initialize the instance variables of another object of the same class. You can use the same value of the instance variables of the object which is sent to the constructor. One object is copied to other objects. Let’s check out this Java Tutorial Copy Constructor in Java language.
Java Tutorial Copy Constructor in Java
- Advantages of using Copy Constructor
- Disadvantages of using Copy Constructor
- Program for Copy Constructor
- Explanation of the Java Code & Output
Advantages of using Copy Constructor
There are several use cases where Copy Constructors works well with respect to other constructors in Java.
As we know, objects can be cloned in Java using the clone() method. But copying object using clone() is not flexible and extensible, doesn’t work fine as well.
The first problem is that no constructor call happens when the object is being cloned. As a result, it is your responsibility, as a writer of the clone method, to make sure all the members have been appropriately set. Here is an example of where things could go wrong.
Consider a class keeping track of the total number of objects of that type, using a static int member. In the constructors you would increase the count. However, if you clone the object, since no constructor is called, the count will not truly reflect the number of objects!
Further, if the class has final fields, these can’t be given a value in the clone method. It leads to problems with properly initializing the object’s last fields. If the final field is referring to some internal state of the object, then the cloned object ends up sharing the internal state, and this surely is not correct for mutable objects.
Disadvantages of using Copy Constructor in Java
As one object is the replica of another, any change in the first one it will inadvertently change the values of the instance variables of other. So you have to be careful and better not to declare copy constructor as public. Otherwise, during inheritance, it doesn’t satisfy the Open-Closed Principle (OCP). The OCP, by Bertrand Meyer, states that software entities should be open for extension, but closed for modifications.
Program for Copy Constructor
class Employee{ String employeeName; String address; int age; double salary; /*Default Constructor */ Employee(){ employeeName = "Platini"; address = "France"; age = 45; salary = 120500.92; } /* Parameterized Constructor */ Employee(String empName,String addr,int ag,double sal){ employeeName = empName; address = addr; age = ag; salary = sal; } /* Copy Constructor */ Employee(Employee emp){ employeeName = emp.employeeName; address = emp.address; age = emp.age; salary = emp.salary; } void showDetails(){ System.out.println("Employee's Name: "+employeeName); System.out.println("Employee's Address: "+address); System.out.println("Employee's Age: "+age); System.out.println("Employee's Salary: "+salary); } } class ConstructorDemo{ public static void main(String args[]){ System.out.println("Employee Details"); System.out.println("----------------"); Employee employee1 = new Employee(); employee1.showDetails(); System.out.println("----------------"); String employeeName = "John"; String address = "Los Angles"; int age = 25; double salary = 34503.92; Employee employee2 = new Employee(employeeName,address,age,salary); employee2.showDetails(); System.out.println("----------------"); Employee employee3 = new Employee(); employee3.showDetails(); System.out.println("----------------"); Employee employee4 = new Employee(employee2); employee4.showDetails(); System.out.println("----------------"); Employee employee5 = new Employee(employee3); employee5.showDetails(); } }
Output
Explanation of the Java Code & Output
Please carefully observe the code marked in red, basically, in the Employee class, you will find the syntax on how to declare copy constructor. We have created two more objects employee 4 and employee5 which are a copy of employee2 and employee3 respectively. From the output, it is visible that instance variables of employee4 have the same value as the values of instance variables of employee2. Same is true for employee5.
Next, we will see how to use the keyword “static” in Java. Check out more useful tutorials and final guidelines on Java programming here.
2 Comments