Java clone () Shallow clone and deep clone

Source: Internet
Author: User

Content transferred from: http://www.blogjava.net/orangelizq/archive/2007/10/17/153573.html

Now clone is not a fresh word, along with the "Dolly" the word is really "fire" over a while, in Java also has such a concept, it can make it very convenient for us to "create" a copy of the object, the following to see how the clone mechanism in Java work?
1. Clone&copy
Suppose there is now an employee object, employee Tobby =new employee ("Cmtobby", 5000), pass
Often we will have such assignment employee cindyelf=tobby, this time just simple copy a bit reference,cindyelf and tobby all point to the same object in memory, Such a cindyelf or tobby operation can affect each other. For example, if we change the value of the salary field through the Cindyelf.raisesalary () method, then tobby through the Getsalary () method is the value of the modified Salary field, which is obviously not what we would like to see. We want to get an exact copy of the Tobby, while the two do not affect each other, and we can use clone to meet our needs. Employee Cindy=tobby.clone (), a new employee object is generated and has the same property values and methods as Tobby.


2. Shallow Clone&deep Clone
How is clone done? Object does not know what to do when cloning an object, it simply executes the domain-to-domain copy, which is shallow Clone. So, here's the problem. For employee, for example, there is a domain hireday is not a basic type of variable, but a reference variable, after cloning will produce a new date type of reference, It points to the same date object as the corresponding field in the original object, so that the Clone class shares part of the information with the original class, which is obviously unfavorable, as shown in the procedure:

At this point we need to do deep clone, special processing of those non-basic domains, such as the hireday in this example. We can redefine the Clone method and do special processing for hireday, as shown in the following code:

1 classEmployeeImplementscloneable2 3 {4          PublicObject Clone ()throwsclonenotsupportedexception5         {6Employee cloned = (employee)Super. Clone ();7Cloned.hireday =(Date) hireday.clone ()8       returncloned;9         }Ten}

3. Protection mechanism of Clone () method

In Object Clone () is declared as protected, and doing so has some justification to the employee

Class as an example, through the declaration as protected, you can ensure that only the employee class inside can "clone" employee object, the principle can refer to my previous about public, protected, private study notes.



4. Use of the Clone () method

The use of the Clone () method is relatively simple, note the following points:

A when to use shallow clone, when to use deep clone, this main look at the specific object of the domain is what nature, basic type or reference variable

b The class to which the object calling the Clone () method belongs must implements the Clonable interface, or clonenotsupportedexception is thrown when the Clone method is called.

Java clone () Shallow clone and deep clone

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.