Unreferenced enclosing class instance ?  
Author Message
lonelyplanet999





PostPosted: 2004-1-13 22:38:00 Top

java-programmer, Unreferenced enclosing class instance ? Hi,

I have below question about below enclosing class instance:

For below class

class MyOuter {
//.. say, some variables and methods defined here
class MyInner {
//.. say, some variables & methods defined here
}
}

pulic class TestNested {
public static void main(String[] args) {
MyOuter.MyInner inner = new MyOuter().new MyInner();
//.. suppose some time & memory consuming code executed afterwards
but inner not assigned 'null'
}
}

If compiled class TestNested ran & garbage collector did run at some
time before program terminated, will the MyOuter instance created in
first line of main be garbage collected ? Is there any method to
re-bind that created MyOuter instance with a reference variable after
the line "MyOuter.MyInner inner = new MyOuter().new MyInner();"
executed (besides split the statement into two like

MyOuter outer = new MyOuter();
MyOuter.MyInner inner = outer.new MyInner() ? :)
 
Dale King





PostPosted: 2004-1-14 0:10:00 Top

java-programmer >> Unreferenced enclosing class instance ? "lonelyplanet999" <email***@***.com> wrote in message
news:email***@***.com...
> Hi,
>
> I have below question about below enclosing class instance:
>
> For below class
>
> class MyOuter {
> //.. say, some variables and methods defined here
> class MyInner {
> //.. say, some variables & methods defined here
> }
> }
>
> pulic class TestNested {
> public static void main(String[] args) {
> MyOuter.MyInner inner = new MyOuter().new MyInner();
> //.. suppose some time & memory consuming code executed afterwards
> but inner not assigned 'null'
> }
> }
>
> If compiled class TestNested ran & garbage collector did run at some
> time before program terminated, will the MyOuter instance created in
> first line of main be garbage collected ? Is there any method to
> re-bind that created MyOuter instance with a reference variable after
> the line "MyOuter.MyInner inner = new MyOuter().new MyInner();"
> executed (besides split the statement into two like
>
> MyOuter outer = new MyOuter();
> MyOuter.MyInner inner = outer.new MyInner() ? :)

There are actually two parts to your question. The first is whether the
enclosing instance will be garbage collected while only the inner instance
is reachable. The answer to that is no. The inner instance has a reference
to the outer instance so the outer instance is not eligible for garbage
collection until any inner class instances are eligible for garbage
collection.

A second part of your question is whether an instance is eligible for
garbage collection when it is only reachable through a local variable and
the local variable is not used anymore in the method, but you haven't
returned from the method. Chris Smith and I debated this question and could
not come to an agreement. I say it is not eligible for GC until the method
finishes and Chris says it is possible to be garbage collected after the
last line of code that uses it. The only way for the difference to be
visible to the program is to have cause some side effect with the finalize
method.