Java copy constructor Example
Java provides the copy constructor much like C++ does. However, it is produced by default in C++. While we define our own copy constructor in Java. With an example, we will learn about Java's copy constructor in this section.
Constructor
The only distinction between a constructor and a method in Java is that the constructor uses the same name as the class. It is utilised to make a class instance. When we create an object belonging to the class, it is immediately called. No return type is present. Keep in mind that a constructor cannot be static, synchronised, final, or abstract. A constructor is inaccessible to us. When called, it takes up some memory space.
Two types of constructors
- The Default constructor
- The parameterized constructor
Java additionally allows the copy constructor in addition to the first two. Let's get into it in more detail.
Why is a copy constructor necessary?
We occasionally run across situations when we need to make an exact duplicate of an already-existing class object. Another requirement is that any changes we make to the copy must not be reflected in the original and vice versa. Java offers you notion of a copy constructor for such circumstances.
Copy constructor
A copy constructor in Java is a unique kind of constructor that makes use of an existing object belonging to the identical Java class to produce a new object. A duplicate of an already-existing class object is what is returned.
The final field can be given a value, but when using the clone() function, this is not possible. If we wish to make the duplicate of an existing object, we use it. Comparatively speaking, it is simpler to implement than the clone() technique.
Note: It can be inherited by the subclasses. When cloning an object of a child class using the copy function Object() { [native code] }, we run into the casting issue if we attempt to initialize it with a parent class reference.
Copy Constructor usage
If we want, we may utilise the copy constructor to:
- Make a duplicate of an object with several fields.
- Make a thorough copy of the bulky items.
- Utilization of the Object.clone() function should be avoided.
Benefits of the Copy Constructor
- A field that has been marked as final may be modified by the copy function Object() { [native code] }.
- Typecasting is not necessary.
- The more fields an item contains, the easier it is to use.
- Because of this, adding a variable towards the class is simple. One area alone needs to be changed: the copy function Object() { [native code] }.
Establishing a Copy Constructor
Use the instructions underneath to generate a copy constructor in Java:
- Make a constructor that takes a parameter that is an object belonging to the same class.
public class Fun
{
private double cost;
private String title;
//The copy constructor
public Fun(Fun fun)
{
//The getters
}
}
- In the instance that was just generated, copy every field (variable) object.
public class Fun
{
private double cost;
private String title;
//The copy constructor
public Fun(Fun fun)
{
// each filed copying
this.cost = fun.cost; //The getter
this.title = fun.title; //The getter
}
}
An illustration of copy constructor
public class Fun
{
private double fcost;
private String ftitle;
// constructor to set the student's name and roll number
Fruit(double fCost, String fName)
{
fprice = fPrice;
ftitle = fTitle;
}
// establishing a copy function Object() { [native code] }
Fun(Fun fun)
{
System.out.println("\nFollowing use of the Copy Constructor:\n");
fcost = fun.fcost;
ftitle = fun.ftitle;
}
// developing a system that returns the cost of the fruit
double showCost()
{
return fcost;
}
// developing a system that returns the cost of the fruit
String showTitle()
{
return ftitle;
}
// creating an object for students in the class that prints their names and roll numbers
public static void main(String args[])
{
Fun f3 = new Fun(299, "Roman Reigns funs");
System.out.println("First fun's title:"+ f3.showTitle());
System.out.println("First fun's cost:"+ f3.showCost());
// supplying the copy constructor with the parameters
Fun f4 = new Fun(f3);
System.out.println("Second fun’s name: "+ f4.showTitle());
System.out.println("Second fruit’s Cost: "+ f4.showCost());
}
}
Output:
First fun's title: Roman Reigns funs
First fun's cost: 299.0
Following use of the Copy Constructor:
Second fun’s title: Roman Reigns funs
Second fun’s cost: 299.0