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 ...