aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes clone Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "clone" Watch "clone" New topic
Author

clone

mo shaik
Greenhorn

Joined: Jul 17, 2007
Posts: 23
hai,
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi,

The clone method is used in cases where you have an object that you need to create a copy of. clone method will return the new object with the same source
state. Where as creating an object using "new" will give completely new
object. Constructor is not called while cloning takes place.


Thanks,


cmbhatt
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Avoid clone:

Make your classes uncloneable. Java's object-cloning mechanism allows an attacker to instantiate a class without running any of its constructors. To make your class uncloneable, just define the following method in each of your classes:



If you really need to make your class cloneable, then there are some protective measures you can take to prevent attackers from redefining your clone method. If you're defining your own clone method, just make it final. If you're not, you can at least prevent the clone method from being maliciously overridden by adding the following:

mo shaik
Greenhorn

Joined: Jul 17, 2007
Posts: 23
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
clone() isn't on the exam


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: clone