

When someone wants to copy a complicated. passing the parameters to the copy constructor A copy constructor is a variant of the constructor method of a class that creates an exact copy of an existing object. ("Price of the first fruit: "+ f1.showPrice()) ("Name of the first fruit: "+ f1.showName()) class to create student object and print roll number and name of the studentįruit f1 = new Fruit(399, "Ruby Roman Grapes") creating a method that returns the name of the fruit creating a method that returns the price of the fruit ("\nAfter invoking the Copy Constructor:\n") constructor to initialize roll number and name of the student

Avoid the use of the Object.clone() method.Generate a deep copy of the heavy objects.Create a copy of an object that has multiple fields.We can use the copy constructor if we want to: If we try to initialize a child class object from a parent class reference, we face the casting problem when cloning it with the copy constructor. Note: It cannot be inherited by the subclasses. It is easier to implement in comparison to the clone() method. It is used if we want to create a deep copy of an existing object.


We can assign a value to the final field but the same cannot be done while using the clone() method. It returns a duplicate copy of an existing object of the class. In Java, a copy constructor is a special type of constructor that creates an object using another object of the same Java class. For such cases, Java provides the concept of a copy constructor. There is also a condition, if we have made any changes in the copy it should not reflect in the original one and vice-versa. Sometimes, we face a problem where we required to create an exact copy of an existing object of the class. Types of ConstructorĮxcept for the above two constructors, Java also supports the copy constructor. It occupies some space in memory when it is called. Remember that a constructor cannot be abstract, final, synchronized, and static. It is called automatically when we create an object of the class. It is used to create an instance of the class. In Java, a constructor is the same as a method but the only difference is that the constructor has the same name as the class name. In this section, we will learn the copy constructor in Java with an example. While in Java we define copy constructor our own. Like C++, Java also supports the copy constructor. int name īut, in a copy constructor accept an object of the current class and initialize the values of instance variables with the values in the obtained object.Next → ← prev Java Copy Constructor Example Usually, to initialize the values of instance variables of a class (one way) we create a parameterized constructor accepting the values for all instance variables and initialize them with the given values. Java supports for copy constructors but unlike C language, Java does not provide an explicit copy constructor you need to define it yourself. Generally, the copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously.
