0

i'm trying to clone a complex object with Cloneable interface.

I have a super object called Resource, that haves two fields:

   public class Resource implements Cloneable{
        protected String name;
        protected boolean isOnline;

        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
   }

Also this class CollectionResource which extends Resource that haves complex fields like for example Cursor, HashMap, DBHelper... etc:

 public class CollectionResource extends Resource{
    DbHelper myDb;
    Cursor currentCursor;
    Cursor tmpCursor;
    HashMap<String, FieldType> fieldsTypes=null;
    private String currenViewName;
    private String currentDateField;    
    boolean datesetMode=false;
    public boolean singleRowMode=false;
    private int lastId=-2;
    int retainCount=0;
    public double lasEditTime=0;

    @Override
    public Object clone() throws CloneNotSupportedException {
        // TODO Auto-generated method stub
        return super.clone();
    }
}

I need to clone a instance of CollectionResource because i need to work with different instances, so i tryed simply doing this:

try {
    this.collection = (CollectionResource) collectionToClone.clone();
} catch (CloneNotSupportedException e) {
    e.printStackTrace();
}

It seems to work... but i don't understand why, because i am using complex objects inside ResourceCollection and i did not implement any kind of deep cloning on my source code...

Why it is working without Deep cloning? Why complex objects like DBHelper, HashMap, Cursor are getting clonned without deep cloning? I suspect that i am not doing it properly and that i will find errors in the near future.

Thanks

NullPointerException
  • 36,107
  • 79
  • 222
  • 382

0 Answers0