简体   繁体   中英

Inner subclass of another inner class - how many refs to the root class?

Suppose I have the structure

class A {
    class B {}
    class C extends B {}
}

It seems that instances of C should have 2 references to the root class A : the first is inherited from B superclass, the second is own implicit inner class reference. My question: does JVM (HotSpot) optimize this case and keep only one reference to the root class?

I decompiled A.class and got this result which shows two references to A, one from B and one from C. But this actually means that C actually has two references - one is its own and the other is inherited from B.

class A {

    class B {
        final A this$0;
        B() {
            this$0 = A.this;
        }
    }

    class C extends B {
        final A this$0;
        C() {
            this$0 = A.this;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM