Mark W
Mark W

Reputation: 2803

Will an inner class get GC'ed if enclosing type reference is maintained?

To me it seems obvious that it wouldn't get maintained, but I've bee fooled before by subtle features of java, like providing a reference to an enclosing type by parameterizing the constructor of the inner class, all the while being ignorant of the syntax EnclosingType.this. I may also be ignorant of some other method of retrieving references to instances of inner classes from their enclosing types. So if I create an instance of an inner class, and don't maintain a reference, but I do maintain the reference to the instance of the enclosing type, will GC ever clean up the inner classes instance?

Upvotes: 2

Views: 113

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500765

Yes, the instance of the inner class will be eligible for GC. The inner class instance has a reference to the instance of the containing class instance, not the other way round.

Upvotes: 2

Related Questions