Introduction to Copy Constructor in Java
Copy Constructors In Java have the same name as the class name, and they do not have any return value like int, float, and decimal. They are very useful in creating objects for a class. Copy Constructor In Java creates the object of an already registered constructor and executes it. Java language is an Object-Oriented Programming Language that supports constructors. A constructor is usually assigned when there is no value assigned to the respective variables. A constructor makes sure that is done. A copy constructor is generally called to initialize the null values in the variable.
A constructor is automatically called at the time of the creation of the object. A copy constructor is easily created when creating the Java Program. In this article, I will present the copy constructor in the Java Blue J platform. Like in the case of a constructor, a copy constructor also doesn’t have a return type, and it has the same name as the class name. It can be a parameterized or a non- parameterized constructor. Copy Constructor is used to creating an exact copy of an object with the same values as an existing object. The constructor doesn’t have any return value. The void is considered one of the return value types, so even a void is not allowed as one of the constructors’ return values.
Working of a Copy Constructor in Java
The working of a Copy Constructor is very similar to that of a constructor. It copies the values of that of a constructor and creates an object. Initially, a class is made in BlueJ by giving the class name. The types of the class name can be variable. It can be an abstract class, Interface, an Applet, a Unit test or an Enum or Enumeration. So the class name is given accordingly, and the constructor’s name is given, which is the same as the class name. This is how we initialize the class in the Java Blue J platform.
In the below-mentioned program, there are two constructors that are made. The two constructors have the same name as the class name, and they do not have any return type. The comments section clearly depicts the two concerned constructors that are being initialized. They are also variables made inside the constructors that are called “re” and “I’m”. The first constructor is a parameterized constructor having “re” as double and “im” as double. With the help of this(), the variables are assigned the values that the user wants to enter. Again another constructor, which is called the copy constructor, is created, also known as Complex like that in the first case. Whenever the copy constructor is called, it prints a message called “Copy Constructor called”. And it stores the values in the object “c”. There is a toString() that also returns a value in the main(). It has a specific printing format that is assigned, and that will be printed.
The below code is shown to be run on the BlueJ platform. BlueJ is one of the most famous platforms for writing code in the Java language. Code can be very easily written and incorporated into Blue J to get desired outputs and results.
The below code shows the main() and the Strings args used to prevent any unknown arguments from and disturb the process. The comments sections clearly depict the different constructors being initialized. The first object is “c1”, where the values 10 and 15 are passed through the parameterized constructor. Object “c2” is also created, and object c1 is passed through the parameterized constructor. Object “c3” is also created with the value of “c2’ in it. Then the c2 is printed and using the print function.
The below code shows the main()being called. The main() would create an object, and if the void main()is called, the program shows the respective output. There are also options to open the editor, inspect the code that we have written, and make changes to suitable code that can be executed on the platform. Also, the following code can be removed from the BlueJ platform by selecting the Remove option on the platform.
Whenever we click on the code, we get the output very smoothly as given. The output smoothly prints the output in a format that has been given in the print statement. The brackets were also given in the print statement.
So the copy constructor is used to make a duplicate of the constructor in an object. Copy constructors are useful in the fact that they can be used to make a clear photocopy of the object of the constructor that we intend to make. Another sample code and output are shown in the below diagram.
The rectangle() asks two values for the length and breadth of the rectangle. The values entered are 6 and 5, respectively.
The value of the area of the rectangle is given in the output tab, as shown below. It returns the value of 30 as the area of the rectangle.
We notice very patiently through this code that the Blue J platform can run constructors as well as copy constructors. In simple terms, a copy constructor is used to create an object of an already existing constructor. Copy constructors are used in many of the programming languages like C++ and Java. The sample Java code clearly depicts the output which has been shown. The “Copy Constructor Called” message has been clearly displayed, and the format of the printing is also shown.
In the second code, the area of the rectangle is printed, whose output is shown above. The Copy Constructor is not used in many programs except for places where a series of complex print statements are there. It is a concept quite similar to recursion, which is used in Java and many of the programming languages. Copy, as the name suggests, is a photocopy of the constructor being used. The article shows and summarizes how in a programming language like BlueJ, a constructor is made, which has the same name as the class name, and a copy or duplicate of it is also created to print a specific statement.
This has been a guide to Copy Constructor In Java. Here we discuss the introduction to Copy Constructor in Java, and it’s working along with Examples. You can also go through our other suggested articles to learn more –