Java internal class this$0 a small bug generated by the field

Source: Internet
Author: User
Tags int size reference

First look at the following piece of code, I pointed out the problem code, the reader first think about what the code will be a problem.

This is a complete copy of the code for a two-item heap (BINOMIALHEAP) structure using the Clone method. The two-item heap contains an internal class Binomialheapentry, the object of this inner class is each node in the two-item heap, which, in addition to the keyword corresponding to the node, records parent parent, the next sibling node sibling, and the first child node three pointers. The root table of the two-item heap is linked by a sibling pointer to each of the two tree root nodes.

The Clonebinomialtree (binomialheapentry root) method recursively copies the entire two-item tree under a root node (including the root node), which traverses each tree node in the root table and copies the corresponding two trees. The new head pointer is then assigned to the new heap after the copy.

public class Binomialheap<e> implements cloneable {transient binomialheapentry head;//The first element in the root table                         int size;
        The size of the entire two-item heap (node number) Private class Binomialheapentry {E element;
        Transient binomialheapentry parent = NULL, child = NULL, sibling = NULL;
     
                transient int degree = 0;
        Private Binomialheapentry (E element) {this.element = element;
        ///Get the child node iterator private iterable<binomialheapentry> children {...} @Override public binomialheap<e> Clone () {//TODO auto-generated method stub Bi
        Nomialheap<e> clone;
        try {clone = (binomialheap<e>) super.clone ();
            catch (Clonenotsupportedexception e) {//TODO auto-generated catch block E.printstacktrace ();
        throw new Internalerror ();
    }         
        Binomialheapentry newhead = null, curroot = NULL;
        Traverse the root table iterator 

First of all, a review of the Java internal class knowledge, Non-static Java inner class as a member of the outer class, can only be accessed through the object of the external class, to create an internal class object, also need to be created by the external class object, namely: Outerobject.new Innerclass (). At this point, the created inner class object produces a field named This$0, which holds the external class object reference that created the inner class object, that is, the outerobject just now. This reference allows an inner class object to freely access the member variables of the external class.

In reviewing the code for the two heap copies above, we tried to create a new node using the new Binomialheapentry () as a node in the new two-item heap when we called the Clonebinomialtree method to copy the two-item tree. But in fact what we actually call is This.new binomialheapentry (), which means that in the new node created, this$0 is pointing to the old two-item heap (this) instead of the new two-item heap (clone) being copied. This allows us to get a copy of the new two-item heap, and through any node of the new two-item heap to access and modify the entire heap of information, the modified is the old two heap, and finally produced a series of strange results.

To fix this bug it is easy to change the binomialheapentry Newroot = Clonebinomialtree (root) in the Clone method to Binomialheapentry Newroot = Clone.clonebinomialtree (Root). So the nodes created in the Clonebinomialtree method are created from the Clone object, and the problem is solved.

The problem is actually relatively simple, and this is really a point to note in programming: When copying an internal class object, consider whether the this$0 field of the copied object points to the external class object you want to point to.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/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.