31 Jan 2013

Copy Constructor in Java

Implement Copy Constructor in Java.

When an object is copied to the other using assignment operator, reference of the object, instead of value of the object, is copied. Therefore changes in one object apply to another.

Copy Constructor can be used to avoid the aforesaid problem.

Java Source Code

class Ball { private String color; private double radius; // A normal constructor public Ball(double radius, String color) { this.radius = radius; this.color = color; } // Copy Constructor Ball(Ball c) { System.out.println("Copy constructor called."); this.radius = c.radius; this.color = c.color; } public void setColor(String color) { this.color = color; } public void setRadius(double radius) { this.radius = radius; } // Overriding the toString of Object class @Override public String toString() { return "Radius: " + this.radius + ", Color: " + this.color; } } public class Box { public static void main(String[] args) { Ball b1 = new Ball(10, "Red"); System.out.println("Original values of b1:"); System.out.println(b1); System.out.println("*******************************************"); /** * Call a copy constructor. * Note that the values of c1 ARE NOT modified. */ Ball b2 = new Ball(b1); b2.setRadius(20); b2.setColor("Black"); System.out.println("After modification of b2 values,"); System.out.println("which is created using copy constructor:"); System.out.println("[b1] " + b1); System.out.println("[b2] " + b2); System.out.println("*******************************************"); /** * Copy by reference. * Note that the values of c1 ARE modified. */ Ball b3 = b1; b3.setRadius(20); b3.setColor("Black"); System.out.println("After modification of b3 values,"); System.out.println("which is created using assignment operator:"); System.out.println("[b1] " + b1); System.out.println("[b2] " + b2); System.out.println("[b3] " + b3); System.out.println("*******************************************"); /** * Changes of c1 values applied to c3. */ b1.setRadius(30); b1.setColor("Pink"); System.out.println("After modification of b1 values:"); System.out.println("[b1] " + b1); System.out.println("[b2] " + b2); System.out.println("[b3] " + b3); System.out.println("*******************************************"); } }

Output

Original values of b1: Radius: 10.0, Color: Red ******************************************* After modification of b2 values, which is created using copy constructor: [b1] Radius: 10.0, Color: Red [b2] Radius: 20.0, Color: Black ******************************************* After modification of b3 values, which is created using assignment operator: [b1] Radius: 20.0, Color: Black [b2] Radius: 20.0, Color: Black [b3] Radius: 20.0, Color: Black ******************************************* After modification of b1 values: [b1] Radius: 30.0, Color: Pink [b2] Radius: 20.0, Color: Black [b3] Radius: 30.0, Color: Pink *******************************************

2 comments:

jeyam said...

your blogs are soo easy to understand,
thanks a lot.. its helping me lot..

continue posting..

if you have any idea in hibernate please start writing ..

or send to jeyasekar18@gmail.com
or
jeyas234@in.ibm.com

jeyam said...

your blogs are soo easy to understand,
thanks a lot.. its helping me lot..

continue posting..

if you have any idea in hibernate please start writing ..

or send to jeyasekar18@gmail.com
or
jeyas234@in.ibm.com