What happens to the Java GC when I go out of scope but still keep the link?

Basically, I'm pretty new to Java, and I'm trying to figure out the following.

public class Test {

TestObject objectA;

 public static void main(String args[])
 {
     Test t = new Test();
     t.test();
     while (true){
         try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {}
         System.out.println("Obj A: " + t.objectA.toString()); 
     }
 }

 void test(){
     Object objectB = new TestObject(1, 1);

     objectA = (TestObject)objectB;
     System.out.println("Obj B: " + objectB);

     objectB = null;     System.out.println("Obj B: " + objectB);
 }

}

Output:

Obj B: TestObject@525483cd 

Obj B: null

Obj A: TestObject@525483cd

Obj A: TestObject@525483cd

Basically, I create a local object of object B in the Method () method and assign it a reference to the variableA object inside the Test object.

Then I set the local variable to zero and check that the link has been canceled.

Now, based on my understanding, I just processed a pointer to an object. An object still exists with an object containing a single reference. Does this mean that the garbage collector will not collect an object created in a method with a narrow scope?

If I drew a pointer to an object, will my object be assembled?

, , , , .

+3
2

, objectA , . , .

, " " "" - , , , , , , JVM .

+5

, , , . , ( 3 , , ):

:

A → B

B → C

C → A

A, B C = > GC.

+1

All Articles