Anatomy of the Clone method in Java

Source: Internet
Author: User


Java also has such a concept, it can make it easy to "make" a copy of the object, below to see how the clone mechanism in Java works?
1. Clone&copy
Suppose there is now an employee object, employee Tobby =new employee ("Cmtobby", 5000), pass
Often we will have this assignment Employee Cindyelf=tobby, this time simply 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:

[Java]View Plaincopyprint?
  1. class Employee implements Cloneable
  2. {
  3. Public Object Clone () throws Clonenotsupportedexception
  4. {
  5. Employee cloned = (employee) Super.clone ();
  6. Cloned.hireday = (Date) hireday.clone ();
  7. return cloned;
  8. }
  9. }

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.

Anatomy of the Clone method in Java

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