This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
What happens, if an instance of an outer class becomes gc-eligible but there are instances of its inner class (in relation with this particular outer class instance) that are still referenced from stack or other objects? Is it possible at all?
I think the problem is that there are a lot of sources that define 'being reachable' as 'there is somewhere a reference variable that points to this object'. And this is the way I always thought of it. And since there may be no direct reference variable pointing to outer class within the inner class, according to such definition it could be possible for an outer class instance to be gc-eligible. But, of course, if we think of being reachable in general sense, it is always possible to reach outer class with 'OuterClass.this' from within inner class... are there any more cases when 'object is not reachable != 'there is no reference variable pointing to it' ?
An instance of an inner class has a reference to the outer class (available through OuterClass.this). So if the instance of the outer class is gc-eligible, then the inner class instance is gc-eligible by definition. If the inner class instance is not gc-eligible then neither is the outer class instance.