This article will demonstrate the approaches to copying an object in Java.
How do I Copy an Object in Java?
An object can be copied in Java using the following approaches:
Access all the classes within the “java.util” package before moving to the examples:
Approach 1: Copying an Object in Java Using “Copy Constructor”
The “Copy Constructor” creates/defines an object by referring to the same class object created previously.
Example
The following example applies the “Copy Constructor” to copy an object:
int id;
String city;
CopyObject(int id, String city){
this.id = id;
this.city = city; }
CopyObject(CopyObject object){
this.id = object.id;
this.city = object.city; }
public void display(){
System.out.println("ID ->"+this.id);
System.out.println("City ->"+this.city); }
public static void main(String[] args) {
Scanner input =new Scanner(System.in);
System.out.println("Enter the id: ");
int id = input.nextInt();
System.out.println("Enter the city: ");
String city = input.next();
CopyObject object1 = new CopyObject(id, city);
CopyObject object2 = new CopyObject(object1);
System.out.println("Default Object ->");
object1.display();
System.out.println("Copied Object ->");
object2.display();
input.close();
}}
In the above code snippet:
- Create a class named “CopyObject”.
- Specify the stated member variables of the class.
- After that, create a parameterized class constructor that allocates the passed values to the specified variables via “this”.
- Now, create a “Copy Constructor” that takes an already created class type object as its parameter that will be passed as an argument later.
- Also, define the function “display()” that displays the passed object values.
- In the “main()” method, make a “Scanner” object to allow user input via the “System.in” parameter.
- Also, associate the “nextInt()” and “next()” methods to take the integer and string user inputs, respectively.
- Create a class object via the “new” keyword and the “CopyObject()” constructor having the user input values as its arguments.
- Also, create a copy object having the created object as its argument to return the copied contents by referring to the class copy constructor.
- Lastly, invoke the “display()” function to display the passed user input values and close the “Scanner”.
Output
In this output, it can be implied that a copy object is created and displayed accordingly.
Approach 2: Copying an Object in Java Via the “clone()” Method
The “clone()” method of the “java.lang.Object” class takes an object as a parameter, creates and gives a copy of it. This method can be implemented to copy an object by referring to the already created object (that needs to be copied) and the “clone()” method.
Syntax
This method gives a copy of the object and throws “CloneNotSupportedException” if the object’s class doesn’t implement/apply the “Cloneable” interface.
Example
Go through the below lines of code:
int id;
String city;
CopyObject(int id, String city){
this.id = id;
this.city = city;
}
public void display(){
System.out.println("ID ->" +this.id);
System.out.println("City ->" +this.city);
}
public static void main(String[] args) throws CloneNotSupportedException {
Scanner input =new Scanner(System.in);
System.out.println("Enter the id: ");
int id = input.nextInt();
System.out.println("Enter the city: ");
String city = input.next();
CopyObject object1 = new CopyObject(id, city);
System.out.println("Default Object ->");
object1.display();
System.out.println("Copied Object ->");
CopyObject object2 = (CopyObject) object1.clone();
object2.display();
input.close();
}}
According to the above code block, perform the below-stated steps:
- Recall the discussed approaches for defining a class, specifying the member variables, creating a parameterized class constructor (allocating the passed values), and defining a function for displaying the values, respectively.
- Now, in “main()”, similarly, the user-input the values and pass these values to the created class object and display them via the corresponding function.
- After that, the “clone()” method comes into effect that is associated with the already created object to create a copy object.
- Lastly, display the copied object contents as well via the discussed function.
Output
In this outcome, it can be observed that the user input values are returned twice with identical outcomes, ensuring that the class object is copied appropriately.
Conclusion
An object can be copied in Java using the “Copy Constructor”, or the “clone()” method. The former approach copies the object by taking the already created object as a copy constructor argument. The latter approach is associated directly with the created class object. This write-up demonstrated the approaches to copy an object in Java.