Making objects eligible for garbage collection

An easy method for making objects available for garbage collection is to assign null to the reference variable that refers to the object. Let's review this example:

    package MyGarbageCollectionSuite;    public class GarbageCollectionExperimentOne     {      public static void main(String[] args)       {        // Declare and create new object.        String junk = new String("Pile of Junk");        // Output to demonstrate that the object        has an active reference        // and is not eligible for garbage collection.        System.out.println(junk);        // Set the reference variable to null.        junk = null;        // The String object junk is now eligible        for garbage collection.      }    }

As indicated in the in-code comments, once the string object reference variable ...

Get Java 9: Building Robust Modular Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.