Clone means make a copy of original. JVM completely clones a object and get a new copy when all members of this object are primitive but when we clone a object that contains class type variable then it can't clone a new object for from this object.We can take a look in this example.User object has a variable of Role class but if we clone User class we get a new instance of user class but Role class remains same.So now we can look it by example.
/** * Created with IntelliJ IDEA. * User: DELL * Date: 7/22/14 * Time: 1:57 PM * To change this template use File | Settings | File Templates. */ public class Role { private long id; private String name; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }******************************************
/** * Created with IntelliJ IDEA. * User: DELL * Date: 7/22/14 * Time: 12:41 PM * To change this template use File | Settings | File Templates. */ public class User implements Cloneable { private long id; private String username; private String password; private Role role; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Role getRole() { return role; } public void setRole(Role role) { this.role = role; } @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } }Now we write a test class to test shallow cloning.Here User class only clonable
and its reference object role is not clonable and its also not cloned in user class when we call the user class
/** * Created with IntelliJ IDEA. * User: DELL * Date: 7/22/14 * Time: 12:35 PM * To change this template use File | Settings | File Templates. */ public class Main { public static void main(String[] args) throws Exception { shallowCloning(); } private static void shallowCloning() throws CloneNotSupportedException { //Shallow Cloning: can not cloning all the Object Types without primitive types System.out.println("This is shallow Cloning Test ::"); User user = new User(); Role role = new Role(); user.setRole(role); user.setId(1); user.setPassword("1111"); user.setUsername("Sanjoy"); user.getRole().setName("Admin"); //clone User Object User user_cloned = (User) user.clone(); user_cloned.setUsername("cloned"); user_cloned.getRole().setName("Manager"); //here both have same output because its does not clone role object. System.out.println("Main User " + user.getRole().getName()); System.out.println("Name Users :: " + user_cloned.getRole().getName()); //Cloning Test //result will be false because they addressed different memory address in heap System.out.println(user.equals(user_cloned)); //Object Reference of both objects System.out.println("Reference of User: "+user); System.out.println("Reference of Cloned User: "+user_cloned); //Here result will be true just get the class name System.out.println(user_cloned.getClass() == user.getClass()); } }The output of this programm like this...
This is shallow Cloning Test ::
Main User Manager
Name Users :: Manager
false
Reference of User: User@14318bb
Reference of Cloned User: User@ca0b6
true
Here Manager will printed both times because we have do it as shallow coding
0 Comments