java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4092
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Use of clone method - cloning objects E-mail
User Rating: / 157
PoorBest 

The reason for making a local copy of an object is if you’re going to modify that object and you don’t want to modify the caller’s object. If you decide that you want to make a local copy, you simply use the clone() method to perform the operation. For example, the standard library class ArrayList overrides clone(), so we can call clone() for ArrayList:

import java.util.*;

class Int {
  
  private int i;
  
  public Int(int ii) { i = ii; }
  
  public void increment() { i++; }
  
  public String toString() {
    return Integer.toString(i);
  }
}

public class DemoCloning {
  
  public static void main(String[] args) {
    ArrayList al = new ArrayList();
    
    for(int i = 0; i < 10; i++ )
      al.add(new Int(i));
    
    System.out.println("al: " + al);

    ArrayList al1 = (ArrayList)al.clone();
    
    // Increment all al1's elements:
    for(Iterator e = al1.iterator(); e.hasNext())  
      ((Int)e.next()).increment();

    // See if it changed al's elements:
    System.out.println("al: " + al);
  }
}

The clone() method produces an Object, which must then be recast to the proper type. This example shows how ArrayList’s clone() method does not automatically try to clone each of the objects that the ArrayList contains—the old ArrayList and the cloned ArrayList are aliased to the same objects. This is often called a shallow copy, since it’s copying only the “surface” portion of an object. The actual object consists of this “surface,” plus all the objects that the references are pointing to, plus all the objects those objects are pointing to, etc. This is often referred to as the “web of objects.” Copying the entire mess is called a deep copy. You can see the effect of the shallow copy in the output, where the actions performed on al1 affect al:

   al: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
   al: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.