Java clone method

Source: Internet
Author: User

What is "clone "?

In the actual programming process, we often encounter this situation: there is an object A, which contains some valid values at a time point, in this case, you may need a new object B that is exactly the same as a, and any subsequent changes to B will not affect the value in A. That is to say, A and B are two independent objects, however, the initial value of B is determined by object. In Java, simple assignment statements cannot meet this requirement. Although there are many ways to meet this requirement, the clone () method is the simplest and most efficient method.

By default, all classes in Java inherit the java. Lang. object class, and there is a method clone () in the Java. Lang. Object Class (). This method returns a copy of the object. There are two points to note: first, the copy object returns a new object instead of a reference. Second, the difference between copying an object and the new object returned using the new operator is that this copy already contains information about the original object, rather than the initial information of the object.

How to apply the clone () method?

A typical call to clone ()CodeAs follows:

Class cloneclass implements cloneable {
Public int aint;
Public object clone (){
Cloneclass o = NULL;
Try {
O = (cloneclass) Super. Clone ();
} Catch (clonenotsupportedexception e ){
E. printstacktrace ();
}
Return O;
}
}

There are three notable points: first, the cloneclass class that can implement the clone function implements the cloneable interface, which belongs to Java. lang Package, Java. the lang Package is already in the default import class, so you do not need to write it as Java. lang. cloneable. Another important note is that the clone () method is overloaded. Finally, super is called in the clone () method. clone (), which also means no matter what the clone class's inheritance structure is, super. clone () directly or indirectly calls Java. lang. object Class clone () method. The following is a detailed explanation of these points.

The third point is the most important. Take a closer look at the clone () native method of the object class. The efficiency of the native method is generally much higher than that of the non-native method in Java. This also explains why to use the clone () method in the object instead of creating a new class, and then assigning the information in the original object to the new object, although this also implements the clone function. For the second point, you should also observe whether the clone () in the object class is a protected attribute method. This also means that if you want to apply the clone () method, you must inherit the object class. All classes in Java inherit the object class by default, so you don't need to care about this. Reload the clone () method. Another consideration is to allow other classes to call the clone () method of the clone class. After the overload, set the attribute of the clone () method to public.

Why should the clone class implement the cloneable interface? Note that the cloneable interface does not contain any methods! In fact, this interface is only a flag, and this flag is only for the clone () method in the object class. If the clone class does not implement the cloneable interface, it calls the clone () method of the object () method (that is, the super. clone () method), the object's clone () method will throw a clonenotsupportedexception exception.

The above are the most basic steps for clone. to complete a successful clone, you also need to know what is "Shadow Clone" and "Deep clone ".

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.