Java clone () Shortest clone and deep clone

Source: Internet
Author: User

The following text is transferred from: Orange Garden

Http://www.blogjava.net/orangelizq/archive/2007/10/17/153573.html

 

 

Now clone is no longer a new word. With the advent of "Dolly", it is indeed a "fire" for a while. This concept also exists in Java, it allows us to easily create a copy of an object. Let's take a look at how the clone mechanism works in Java?
1. Clone & copy
Assume that there is currently an employee object, and the employee Tober = new employee ("cmtober", 5000 ),
Usually, we will assign the value 'employee cindyelf = toby'. At this time, we just copied the reference. Both cindyelf and toby' point to the same object in the memory, in this way, an operation of cindyelf or Tober may affect the other party. For example, if we use cindyelf. the raisesalary () method changes the value of the salary domain, so what tolobby obtains through the getsalary () method is the value of the modified salary domain. Obviously, this is not what we are willing to see. We hope to get a precise copy of Tober, and the two do not affect each other. At this time, we can use clone to meet our needs. Employee
Cindy = Tober. Clone (), a new employee object is generated, and the value and method of the new employee are the same as that of the former.
2. Shallow clone & Deep clone
How is clone completed? Objects do not know anything about them when performing clone on an object. They simply execute the copy of the domain to the domain, which is shallow clone. In this case, the problem arises. Taking employee as an example, there is a reference variable in the hireday field, which is not a basic variable, after cloning, a new date type reference is generated, which directs 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, this is obviously unfavorable, as shown in the process:

 

In this case, we need to perform deep clone and perform special processing on non-basic fields, such as hireday in this example. We can redefine the clone method and perform special processing on hireday, as shown in the following code:Class employee implements cloneable </P> <p >{< br/> Public object clone () throws clonenotsupportedexception <br/>{< br/> employee cloned = (employee) super. clone (); <br/> cloned. hireday = (date) hireday. clone () <br/> return cloned; <br/>}< br/>

 

3. Clone () method protection mechanism

In the object, clone () is declared as protected. In this case, there is a certain reason,

For example, if you declare it as protected, you can ensure that only the employee class can "clone" the employee object. For the principle, refer to my previous study notes on public, protected, and private.

4. Use the clone () method

The clone () method is easy to use. Pay attention to the following points:

A. When to use shallow clone and deep clone depends on the nature of the object's domain. The basic type is still reference variable.

B. The class of the object that calls the clone () method must be implements clonable. Otherwise, clonenotsupportedexception will be thrown when the clone method is called.

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.