The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2002

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

cloning

Posted by Mukul on January 20, 2002 at 11:57 PM

> I have to create 10 objects in a for loop. I am considering to do it in two ways. One is explicitly create the object with new operator. The second one is to create the object with new operator once and clone it 9 times. (Of cource I will call the setXXX methods of the class to change the values after cloning). Which stategy is better? Creating 10 times with new or cloning the object.
> To make the situation clear I am putting little code too.

> for (loop:10 times) {
> x x1 = new x();
> x1.setXXX(value);
> x1.setYYY(value);
> // add object to Vector
> }

> OR

> x x1 = new x();
> x1.setXXX(value);
> x1.setYYY(value);
> // add object to Vector

> for (loop:9 times) {
> x x2 = (x)x1.clone();
> x2.setXXX(value);
> x2.setYYY(value);
> // add object to Vector
> }

> Please advise.


This article provide good information...

http://developer.java.sun.com/developer/JDCTechTips/2001/tt0306.html



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us