Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clone the object in java

org.apache.cxf.jaxrs.ext.multipart.Attachment attachments=null;

List<Attachment> clone = new ArrayList<Attachment>(attachments.size());
for(Object item: attachments) 
clone.add((Attachment)item.clone());//The method clone() from the type Object is not visible

I want to clone the List<Attachment> clone object but it saying The method clone() from the type Object is not visible

like image 909
Prasad V S Avatar asked Sep 18 '26 22:09

Prasad V S


1 Answers

Do not use Java standard clone method from Object. At least not before you read what Josh Bloch says about cloning in his book Effective Java.

You need to do a deep copy in this case i.e create an empty ArrayList of Attachment objects (which you did) and copy the values of all the fields of every Attachment object.

like image 159
aviad Avatar answered Sep 20 '26 11:09

aviad