This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Java in General and the fly likes method-local inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "method-local inner classes" Watch "method-local inner classes" New topic

method-local inner classes

Harry Tuttle

Joined: Apr 08, 2010
Posts: 24

I'm just preparing for the scjp/ocp and in the chapter about the inner classes (SCJP Sun Certified Programmer for Java 6 Study Guide I, p. 670) there is this code example:

I understand all lines, but my question is: It's right that the inner object will be carbage collected (but with no garantee) after the method doStuff() completes?

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15001

Since the variable inner goes out of scope at the end of the method, there are no other references to the object and the object is not passed out of the method, then yes, it's eligible for garbage collection.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: method-local inner classes
It's not a secret anymore!