英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论