How do we copy objects in java?

Java 8Object Oriented ProgrammingProgramming

In Java you can copy an object in several ways, among them, copy constructor and the clone method are the mostly used.

Using copy constructor

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. Java does support for copy constructors but you need to define them yourself.

Example

In the following Java example, we a have a class with two instance variables name and age and a parameterized constructor initializing these variables.

Then, we have another constructor which accepts an object of the current class and initializes the instance variables with the variables of this object.

If you instantiate this class using the second constructor by passing an object to it, this results an object which is the copy of the one which you passed as an argument.

 Live Demo

import java.util.Scanner;
public class Student {
   private String name;
   private int age;
   public Student(String name, int age){
      this.name = name;
      this.age = age;
   }
   public Student(Student std){
      this.name = std.name;
      this.age = std.age;
   }
   public void displayData(){
      System.out.println("Name : "+this.name);
      System.out.println("Age : "+this.age);
   }
   public static void main(String[] args) {
      Scanner sc =new Scanner(System.in);
      System.out.println("Enter your name ");
      String name = sc.next();
      System.out.println("Enter your age ");
      int age = sc.nextInt();
      Student std = new Student(name, age);
      System.out.println("Contents of the original object");
      std.displayData();
      System.out.println("Contents of the copied object");
      Student copyOfStd = new Student(std);
      copyOfStd.displayData();
   }
}

Output

Enter your name
Krishna
Enter your age
20
Contents of the original object
Name : Krishna
Age : 20
Contents of the copied object
Name : Krishna
Age : 20

Using the clone method

The clone() method of the class java.lang.Object accepts an object as a parameter, creates and returns a copy of it.

Example

In the following Java example, we a have a class with two instance variables name and age and a parameterized constructor initializing these variables.

From the main method we are creating an object of this class and generating a copy of it using the clone() method.

 Live Demo

import java.util.Scanner;
public class CloneExample implements Cloneable {
   private String name;
   private int age;
   public CloneExample(String name, int age){
      this.name = name;
      this.age = age;
   }
   public void displayData(){
      System.out.println("Name : "+this.name);
      System.out.println("Age : "+this.age);
   }
   public static void main(String[] args) throws CloneNotSupportedException {
      Scanner sc =new Scanner(System.in);
      System.out.println("Enter your name ");
      String name = sc.next();
      System.out.println("Enter your age ");
      int age = sc.nextInt();
      CloneExample std = new CloneExample(name, age);
      System.out.println("Contents of the original object");
      std.displayData();
      System.out.println("Contents of the copied object");
      CloneExample copiedStd = (CloneExample) std.clone();
      copiedStd.displayData();
   }
}

Output

Enter your name
Krishna
Enter your age
20
Contents of the original object
Name : Krishna
Age : 20
Contents of the copied object
Name : Krishna
Age : 20
raja
Published on 08-Jul-2019 15:29:33
Advertisements