Results 1 to 2 of 2
  1. #1
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default Overriding .clone() Method

    Hi,

    I'm curious of whether this methodology is a quick, memory efficent, or all around good way to .clone() a large object. The general objects that I clone have a large collection of smaller objects attached to it.

    Java Code:
        public Object clone()
                throws CloneNotSupportedException {
    		MyObjectArray obj = (MyObjectArray)super.clone();
    		obj.itemList = null;
    		if ( itemList != null ) {
    			obj.itemList = new Vector(itemList.size());
    			Enumeration e = itemList.elements();
    			while ( e.hasMoreElements() ) {
    				MyObject element = (MyObject)e.nextElement();
    				obj.itemList.addElement( element.clone() );
    			}
    		}
    		return obj;
        }
    Note: itemList is a vector of my current Class.
    The most important thing to me currently is using a lower memory footprint.

    Thanks for any feedback.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,432
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Moncleared View Post
    Hi,

    I'm curious of whether this methodology is a quick, memory efficent, or all around good way to .clone() a large object. The general objects that I clone have a large collection of smaller objects attached to it.

    Java Code:
        public Object clone()
                throws CloneNotSupportedException {
    		MyObjectArray obj = (MyObjectArray)super.clone();
    		obj.itemList = null;
    		if ( itemList != null ) {
    			obj.itemList = new Vector(itemList.size());
    			Enumeration e = itemList.elements();
    			while ( e.hasMoreElements() ) {
    				MyObject element = (MyObject)e.nextElement();
    				obj.itemList.addElement( element.clone() );
    			}
    		}
    		return obj;
        }
    Note: itemList is a vector of my current Class.
    The most important thing to me currently is using a lower memory footprint.

    Thanks for any feedback.
    Yep, that's a fine way to implement a 'deep' clone (don't forget to implement the empty interface Cloneable). It all depends on your needs whether or not you want a deep clone (copy) or a shallow clone.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Clone method question
    By frenk_castle in forum New To Java
    Replies: 3
    Last Post: 04-08-2010, 08:17 PM
  2. Method Overriding - Seriously confused :-(
    By fullmetaljacket in forum New To Java
    Replies: 4
    Last Post: 05-26-2009, 04:18 PM
  3. clone method
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 01-30-2008, 09:47 AM
  4. is overriding static method possible
    By raghu in forum Advanced Java
    Replies: 1
    Last Post: 01-22-2008, 12:38 AM
  5. clone method
    By gapper in forum New To Java
    Replies: 1
    Last Post: 01-20-2008, 08:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •