内部类在内存中的位置在哪里?

huangapple go评论66阅读模式
英文:

Where is the inner class in the memory?

问题

我们知道类通常会被加载到元空间(meta space)中,但是非静态内部类呢?当非静态内部类被加载后会去哪里?

class OuterClass {
    /**
     * 这个类在哪里?这个类会被加载到元空间吗?
     */
    class InnerClass{
        public void method(){
            /**
             * 那么这个类在哪里?
             */
            class InnerClassInMethod{
                public void method(){
                    System.out.println("Hello");
                }
            }

            new LocalClass().method();
        }
    }
}

那么类会在什么时候被加载?会和外部类一起加载,还是在实例被请求时才会被延迟加载?而对于它们的卸载呢?

英文:

We know a Class is usually been loaded into meta space, but what about an nonstatic inner class? where it goes when it has been loaded?

class OuterClass {
    /**
     * Where is this class? would this class goes to the meta space?
     */
    class InnerClass{
        public void method(){
            /**
             * And where is this class?
             */
            class InnerClassInMethod{
                public void method(){
                    System.out.println("Hello");
                }
            }

            new LocalClass().method();
        }
    }
}

And when the class would be loaded? it would be loaded as well as the outer class or deferred until an instance has been requested? And, same question for they are going to be unloaded?

答案1

得分: 1

MetaSpace中也是如此。static类和non-static类之间唯一的区别是non-static类包含对父类的引用this

英文:

In the MetaSpace as well. The only one difference between static and non-static classes is non-static class contains reference this to the parent class.

huangapple
  • 本文由 发表于 2020年8月22日 03:25:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63528977.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定