Here, the shallow copy means it creates a new object and copies all the fields and methods associated with the object.
The syntax of the clone()
method is:
object.clone()
clone() Parameters
The clone()
method does not take any parameters.
clone() Return Values
- returns the copy of the object
- throws
CloneNotSupportedException
if the object's class does not implement theCloneable
interface
Note: The Object
class does not implement Cloneable
. Hence, we cannot call the clone()
method for the object of the Object
class.
Example 1: Java clone()
class Main implements Cloneable {
// declare variables
String name;
int version;
public static void main(String[] args) {
// create an object of Main class
Main obj1 = new Main();
// initialize name and version using obj1
obj1.name = "Java";
obj1.version = 14;
// print variable
System.out.println(obj1.name); // Java
System.out.println(obj1.version); // 14
try {
// create clone of obj1
Main obj2 = (Main)obj1.clone();
// print the variables using obj2
System.out.println(obj2.name); // Java
System.out.println(obj2.version); // 14
}
catch (Exception e) {
System.out.println(e);
}
}
}
In the above example, we have created a class named Main. The class includes two fields name and version. Here, we have initialize the class fields using the object obj1 of the class.
Notice the line,
Main obj2 = (Main)obj1.clone();
Here, we have used the clone()
method to create copy of obj1. The value returned by clone()
is assigned to the object obj2. Since the return value of clone()
is Object
type, we have used (Main)
to convert it into Main type.
Now the fields name and version can be accessed using the object obj2. However if we change the value of the fields using obj2, the value associated with obj1 won't be changed.
Example 2: Changing value using cloned object
class Main implements Cloneable {
// declare variables
String name;
int version;
public static void main(String[] args) {
// create an object of Main class
Main obj1 = new Main();
// initialize name and version using obj1
obj1.name = "Java";
obj1.version = 14;
// print variable
System.out.println(obj1.name); // Java
System.out.println(obj1.version); // 14
try {
// create a clone of obj1
Main obj2 = (Main)obj1.clone();
// print the variables using obj2
System.out.println(obj2.name); // Java
System.out.println(obj2.version); // 14
// changing value of name
// using obj2
obj2.name = "Python";
System.out.println(obj2.name); // Python
// check if value associated
// with obj1 is changed
System.out.println(obj1.name); // Java
}
catch (Exception e) {
System.out.println(e);
}
}
}
Notice the line,
obj2.name = "Python";
Here, we have changed the value of the variable using obj2. Now when we print the variable name for both obj1 and obj2, we can see that the value for obj1 has not changed. It is because the clone()
method makes the shallow copy of the objects.
To learn more on shallow copy, visit What is Shallow Copy in Java?
We have enclosed the clone()
method inside the Java try...catch block. It is because subclass can throw the exception if the object cannot be cloned.
Note: The Object
class is the superclass for all the classes in Java. Hence, every class and arrays can implement the clone()
method.
Also Read: