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:

Don't think there is a difference

Posted by Hiran on January 19, 2002 at 10:42 AM

As far as I can see, there is no difference. I haven't used the clone method before, but if the clone method clones the whole object (and not just the reference), then it shouldn't matter. There might be memory issues that are also involved, but I know of none. In this case I would probably go with the first method (creating the object 10 times) because it's less code. Any differing opinions?
Hiran

> 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.






Replies:

Sponsored Links



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