The question is: Why is there no Garbage Collection after
b1 = null; ???
Result: Compiles and runs with:
C:\Java\EigeneJavaProgramme>
java ObjectGarbageCollectionTest1b
Object1 Object2
Object2 Object2
WHY ist there now a Garbage Collection after
b1= null??
public class ObjectGarbageCollectionTest1b {
int i;
ObjectGarbageCollectionTest1b(int i) {this.i=i;} //Constructor
//
String toString() {return "Object"+ i;}
public String toString() {return "Object"+ i;}
public static void main (String[] argv) {
ObjectGarbageCollectionTest1b b1 = new ObjectGarbageCollectionTest1b(8);
ObjectGarbageCollectionTest1b b2 = new ObjectGarbageCollectionTest1b(2);
b1 = null;
// b1 = b2;
System.out.println("Object1 "+b1.toString());
System.out.println("Object2 "+b2.toString());
}
}
Result: Compilation Error:
C:\Java\EigeneJavaProgramme>java ObjectGarbageCollectionTest1b
java.lang.NullPointerException
at ObjectGarbageCollectionTest1b.main(ObjectGarbageCollectionTest1b.java:14)
Exception in
thread "main"