简体   繁体   English

在外部类外部创建内部类对象时引用外部类对象

[英]Outer class object reference when Inner class object is created outside the Outer class

Inner classes can be instantiated outside the outer class using Outer_class_object.new inner_class() . 可以使用Outer_class_object.new inner_class()在外部类外部实例化内部类。 But the question arises what happens to Outer class object? 但是问题出在外类对象身上会发生什么? means there is no reference variable referencing to it. 意味着没有引用变量引用它。 Obviously it will be on the heap till the inner class object is on heap because Inner classes feed on outer class. 显然,它将一直在堆上,直到内部类对象在堆上为止,因为内部类以外部类为食。 So what mechanism java uses in such situations? 那么java在这种情况下使用什么机制? See the following code... 请参阅以下代码...
What happens to the outer class object in the main of NormalInnerClassDemo that we used to create object of inner class. 我们用来创建内部类对象的NormalInnerClassDemo主体中的外部类对象会发生什么。 It has to stay alive till the 'inner' is alive.There is no reference variable referring to it doesn't that make it eligible for garbage collection ? 它必须一直活到“内部”还活着。没有引用变量引用它是否使它有资格进行垃圾回收? If not how does Java manages to tell the garbage collector not to do so ? 如果不是,Java如何设法告诉垃圾回收器不要这样做?

class Outer {

    private String  string = "Google";

    class Inner {

        public Inner() {
            System.out.println("From Inner:" + string );
        }
    }

    public void test() {
        //Some code here
    }
}

public class NormalInnerClassDemo {

    public static void main( String[] args ) {
        Outer.Inner inner = new Outer().new Inner();
    }
}

Inner classes can be instantiated outside the outer class using Outer_class.inner_class. 可以使用Outer_class.inner_class在外部类外部实例化内部类。

No they can't. 不,他们不能。 They are created via: 它们是通过以下方式创建的:

outer.new Outer.Inner(...)

where outer is a primary-expression that evaluates to a reference to an instance of Outer , for example new Outer(...) . 其中outer是主要表达式,其计算结果是对Outer实例的引用,例如new Outer(...)

or 要么

this.new Inner(...)

if you are inside Outer , or for short 如果您在Outer ,或简称

new Inner(...)

if you are inside Outer . 如果你在Outer内部。

But the question arises what happens to Outer class object? 但是问题出在外类对象身上会发生什么? means there is no reference variable referencing to it. 意味着没有引用变量引用它。

Oh yes there is: see above. 哦,是的,有:见上文。

Obviously it will be on the heap till the inner class object is on heap because Inner classes feed on outer class. 显然,它将一直在堆上,直到内部类对象在堆上为止,因为内部类以外部类为食。 So what mechanism java uses in such situations? 那么java在这种情况下使用什么机制?

Your question is founded on a mistake. 您的问题基于错误。

First this is Java so why are you worried about heap memory when the VM cleans up for you? 首先,这是Java,所以当VM为您清理时,为什么还要担心堆内存? 8-) 8-)

To help you understand this, ADD the following to Outer class (default constructor): 为了帮助您理解这一点,请将以下内容添加到Outer类(默认构造函数)中:

Outer() {
  System.out.println("Outer()");
}

Then run the program and you will see Outer is created too. 然后运行程序,您还将看到外部也被创建。 Glad you are interested in how/what's created. 很高兴您对创建方式感兴趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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