File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Architect Certification (SCEA/OCMJEA) and the fly likes prototype pattern and clone() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Architect Certification (SCEA/OCMJEA)
Bookmark "prototype pattern and clone()" Watch "prototype pattern and clone()" New topic

prototype pattern and clone()

Peter Warde
Ranch Hand

Joined: Aug 18, 2004
Posts: 71
From my understand the prototype pattern in Java means clone(). What is exactly the difference between myClass.clone() and new MyClass()?
Brian Mozhdehi
Ranch Hand

Joined: Aug 17, 2006
Posts: 81
Not much difference. If you implement clone(), you should instantiate a new instance of the class and return it. In theory I suppose you could do just that (really, you CAN return any Object you want to), but what generally is done is some sort of carryover of the attributes of the class that is being cloned into a new instance of the class.

Useful IMHO for encapsulation.
Virendran Pushpanayagam
Ranch Hand

Joined: Jun 12, 2004
Posts: 31

For example you have an Object that is very costly to create (new) in that case you use the clone function. The benefit is that you don�t have to go through all the trouble of creating a complex Object again.

I agree. Here's the link:
subject: prototype pattern and clone()
It's not a secret anymore!