Pointers, links, object and reference count

Posted by EugeneP on Stack Overflow See other posts from Stack Overflow or by EugeneP
Published on 2010-03-29T09:50:25Z Indexed on 2010/03/29 9:53 UTC
Read the original article Hit count: 238

Filed under:

String a = "a"; // allocate memory and write address of a memory block to a variable String b = "b";

// in a and b hold addresses

b = a; // copy a address into b.

// Now what? b value is completely lost and will be garbage collected

//* next step

a = null; // now a does not hold a valid address to any data,

// still data of a object exist somewhere, yet we cannot get access to it.

Correct me if there's a mistake somewhere in my reflexions.

My question is:

suppose anInstance object of type Instance has a property ' surname '
anInstance.getSurname() returns "MySurname".
now String s = anInstance.getSurname();
anInstance = null;

question is - is it true that getSurname value, namely MySurname
will not be garbage collected because and only because
it has active reference
counter > 0, and if other properties of anInstance 
have a zero reference counter, they'll be
garbage collected?

© Stack Overflow or respective owner

Related posts about java