Copy constructors and protective copy

What is a copy constructor ?

Can someone share a small example that may be useful for understanding along with the principle of protective copying ?

+7
source share
5 answers

Here is a good example:

class Point { final int x; final int y; Point(int x, int y) { this.x = x; this.y = y; } Point(Point p) { this(px, py); } } 

Notice how the Point(Point p) constructor takes Point and makes a copy of it - a copy constructor .

This is a defensive copy because the original Point is protected from change by taking a copy of it.

So now:

 // A simple point. Point p1 = new Point(3,42); // A new point at the same place as p1 but a completely different object. Point p2 = new Point(p1); 

Note that this is not necessarily the correct way to create objects. This, however, is a good way to create objects, which ensures that you never have two references to the same object by accident. Clearly, this is only good if that is what you want to achieve.

+9
source

Copy the constructors that are often visible in C ++, where they are needed for partially hidden, automatically called operations.

java java.awt.Point and Rectangle come to mind; also very old, mutable objects.

Using immutable objects such as String or BigDecimal , just assign a reference to the object. In fact, due to the early phase of Java after C ++, there is still a dumb copy constructor in the String :

 public class Recipe { List<Ingredient> ingredients; public Recipe() { ingredients = new ArrayList<Ingredient>(); } /** Copy constructor */ public Recipe(Recipe other) { // Not sharing: ingredients = other.ingredients; ingredients = new ArrayList<Ingredient>(); ingredients.addAll(other.ingredients); } public List<Ingredient> getIngredients() { // Defensive copy, so others cannot change this instance. return new ArrayList<Ingredient>(ingredients); // Often could do: // return Collections.immutableList(ingredients); } } 
+6
source

Copy constructor in java can be used when you need to clone an object

 class Copy { int a; int b; public Copy(Copy c1) { a=c1.a; b=c1.b; } } 

In java, when you give Copy c2=c1 ; just creates a link to the source object, not a copy, so you need to manually copy the values โ€‹โ€‹of the object.

See this:

+1
source

The copy constructor is used to create a new object using the values โ€‹โ€‹of an existing object.
One possible use case is to protect the original object from modification, while the copied object can be used for work.

 public class Person { private String name; private int age; private int height; /** * Copy constructor which creates a Person object identical to p. */ public person(Person p) { person = p.person; age = p.age; height = p.height; } . . . } 

In connection with a protective copy , it is well read

+1
source

Here you create a new object, passing the old object, copying its values.

 Color copiedColor = new Color(oldColor); 

instead:

 Color copiedColor = new Color(oldColor.getRed(), oldColor.getGreen(), oldColor.getBlue()); 
0
source

All Articles