We'll generally create clone, when we want to create local copy of the object and don't want to modify the original object.
Use the clone() method cautiously, or else you can also create a copy constructor alternative to the clone().
This is a protected method in java.lang.Object class.
protected Object clone() CloneNotSupportedException : It creates and returns a copy of this object.
By default shallow copy is applied. If we want extra logic to be impemented then we need to opt for deep copy.
Shallow copy : Just call the super.clone()
Deep copy : We need to implement ourselves.
A simple example
describing clone() method in Java:
Account.java
Use the clone() method cautiously, or else you can also create a copy constructor alternative to the clone().
This is a protected method in java.lang.Object class.
protected Object clone() CloneNotSupportedException : It creates and returns a copy of this object.
By default shallow copy is applied. If we want extra logic to be impemented then we need to opt for deep copy.
Shallow copy : Just call the super.clone()
Deep copy : We need to implement ourselves.
A simple example
describing clone() method in Java:
Account.java
package com.clone.sample; public class Account implements Cloneable { public Account(long acc_num,String name,String branch) { this.setAcc_num(acc_num); this.setName(name); this.setBranch(branch); } private long acc_num; private String name; private String branch; public void setAcc_num(long acc_num) { this.acc_num = acc_num; } public void setName(String name) { this.name = name; } public void setBranch(String branch) { this.branch = branch; } public Object clone() throws CloneNotSupportedException { return super.clone(); } public String toString() { return "Acct No: " this.acc_num "\n" "Acct.holder's name : " name "\n" "Branch : " branch; } }
CloneSample.java
package com.clone.sample; public class CloneSample { public static void main(String[] args) { Account acc = new Account(68029,"Praveen","Hyderabad"); System.out.println(acc); try { Account copyAcc = (Account) acc.clone(); System.out.println("Shallow copy : \n" "[" copyAcc "]"); System.out.println(acc.clone().getClass() == copyAcc.getClass()); // instances of same class System.out.println(acc.equals(copyAcc)); System.out.println(acc.clone()!= copyAcc); } catch (CloneNotSupportedException e) { e.printStackTrace(); } } }
output:
Acct No: 68029
Acct.holder's name : Praveen
Branch : Hyderabad
Shallow copy :
[Acct No: 68029
Acct.holder's name : Praveen
Branch : Hyderabad]
true
false
true
protected Object clone() throws CloneNotSupportedException
ReplyDeleteHаνing reaԁ this I thought it was very enlіghtening.
ReplyDeleteΙ aρpгeciate уou taking the time and energу to ρut this
informativе аrticlе togetheг.
I oncе agаin find mуself spеnding wаy tоо
much timе both геading and posting commentѕ.
Βut so what, it waѕ still wοrth it!
my blog ροѕt; one hour payday loans
Geneгallу I do not read агtiсlе on blogs, however I wοulԁ likе to ѕaу that thiѕ wгіtе-uρ vеry
ReplyDeletepressured me tο tгy and do it! Үοuг writіng taѕte has bеen ѕurpгiѕed
me. Thank you, quite nicе article.
my webpagе :: international reputation management