Java Object clone() Method



Description

The Java Object clone() method creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. The general intent is that, for any object x, the expression −

 x.clone() != x

will be true, and that the expression −

x.clone().getClass() == x.getClass()

will be true, but these are not absolute requirements. While it is typically the case that −

 x.clone().equals(x)

will be true, this is not an absolute requirement.

Declaration

Following is the declaration for java.lang.Object.clone() method

protected Object clone()

Parameters

NA

Return Value

This method returns a clone of this instance.

Exception

CloneNotSupportedException − if the object's class does not support the Cloneable interface. Subclasses that override the clone method can also throw this exception to indicate that an instance cannot be cloned.

Getting Clone of a GregorianCalendar Object Example

The following example shows the usage of java.lang.Object.clone() method. In this program, we've created an instance of GregorianCalendar and then using clone() method, we've created another object using earlier object. Time from both the object is printed.

package com.tutorialspoint;

import java.util.GregorianCalendar;

public class ObjectDemo {

   public static void main(String[] args) {

      // create a gregorian calendar, which is an object
      GregorianCalendar cal = new GregorianCalendar();

      // clone object cal into object y
      GregorianCalendar y = (GregorianCalendar) cal.clone();

      // print both cal and y
      System.out.println(cal.getTime());
      System.out.println(y.getTime());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Fri May 31 13:50:19 IST 2024
Fri May 31 13:50:19 IST 2024

Getting Clone of a ArrayList Object Example

The following example shows the usage of java.lang.Object.clone() method. In this program, we've created an instance of ArrayList and then using clone() method, we've created another object using earlier object. Both the objects are printed.

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.List;

public class ObjectDemo {

  public static void main(String[] args) {

    // create a ArrayList, which is an object
    ArrayList<String> list = new ArrayList<>();

    // add value to the list
    list.add("Tutorialspoint");

    // clone object list into object y
    List<String> y = (List) list.clone();
  
    // print both list and y
    System.out.println(list);
    System.out.println(y);
  }
}

Output

Let us compile and run the above program, this will produce the following result −

[Tutorialspoint]
[Tutorialspoint]

Getting Clone of a HashMap Object Example

The following example shows the usage of java.lang.Object.clone() method. In this program, we've created an instance of HashMap and then using clone() method, we've created another object using earlier object. Both the objects are printed.

package com.tutorialspoint;

import java.util.HashMap;

public class ObjectDemo {

   public static void main(String[] args) {

      // create a HashMap, which is an object
      HashMap<String, String> map = new HashMap<>();

      // add a value to the map
      map.put("t", "tutorialspoint");
      
      // clone object map into object y
      HashMap<String, String> y = (HashMap)map.clone();

      // print both map and y
      System.out.println(map);
      System.out.println(y);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

{t=tutorialspoint}
{t=tutorialspoint}
java_lang_object.htm
Advertisements