Clone in Java ()

Source: Internet
Author: User

What is "clone"?

In the actual programming process, we often encounter this situation: there is an object A, in a moment a is already included in a number of valid values, it may be necessary to a exactly the same new object B, and then any changes to B will not affect the value of a, that is, A and B is two separate objects, But the initial value of B is determined by the A object. In the Java language, this requirement cannot be satisfied with simple assignment statements. There are many ways to meet this requirement, but the implementation of the Clone () method is the simplest and most efficient means.

All Java classes inherit the Java.lang.Object class by default, and there is a Method clone () in the Java.lang.Object class. The documentation for the JDK API explains that this method will return a copy of the object. There are two points to note: one is that the Copy object returns a new object, not a reference. Second, the difference between a copy object and a new object returned with the newly operator is that the copy already contains information about the original object, not the object's initial information.

How do I apply the Clone () method?

A typical invocation of the clone () code is as follows:

class Cloneclass implements CLONEABLE{ 
public int aint; ,
Public Object clone () { 
cloneclass o = null; 
try{ 
o = (clone Class) Super.clone ();  
}catch (clonenotsupportedexception e) { 
E.printstacktrace ();  
}  
return o; 

}


There are three noteworthy places, one is to realize the clone function of the Cloneclass class implemented the Cloneable interface, this interface belongs to the Java.lang package, The Java.lang package is already in the default import class, so there is no need to write java.lang.Cloneable. Another worthwhile note is that the clone () method is overloaded. Finally, Super.clone () is called in the Clone () method, which means that super.clone () directly or indirectly calls the Clone () method of the Java.lang.Object class, regardless of the inheritance structure of the Clone class. Here is a detailed explanation of these points.

It should be said that the 3rd is the most important, a closer look at the object class clone () A native method, the efficiency of the native method is generally much higher than the non-native method in Java. This also explains why the clone () method in object is used instead of a new class, and then the information in the original object is assigned to the object, although this also implements the Clone function. For the 2nd, it is also a way to observe the clone () or a protected property in the object class. This also means that if you want to apply the Clone () method, you must inherit the object class, and all classes in Java inherit the object class by default, and you don't have to care about that. The Clone () method is then overloaded. Another thing to consider is to make the Clone () method of the clone class available to other classes, and to set the Clone () method's property to public after overloading.

So why does the Clone class also implement the Cloneable interface? A little attention, cloneable interface is not included in any method! In fact, this interface is only a flag, and this flag is only for the object class in the Clone () method, if the clone class did not implement the Cloneable interface, and called the object's Clone () method (that is, called super. Clone () method, the Clone () method of object throws a Clonenotsupportedexception exception.

The above is the most basic step in cloning, to complete a successful clone, but also to understand what is "shadow clone" and "depth clone".

Clone in Java ()

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.