Even though the above statements create circular reference indirectly, both the objects pointed by reference c1 and c2 will become eligible for garbage collection after the following code get executed.
For your reference .. Circular reference is nothing but two objects having reference of each other.
Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name.
Variables are not objects. Variables just hold references to objects.
First, two objects are created: A new instance of MyClass whose reference is assigned to the variable c1, and another new instance of MyClass whose reference is assigned to the variable c2. Remember, c1 and c2 are just variables pointing to these objects.
Each of these objects has an instance variable called z, which is initialized to null.
By calling the "other" method, c1.z is assigned a reference to the object referenced by c2, and c2.z is assigned a reference to the object referenced by c1.
Next, a third object is created: A new instance of MyClass whose reference is assigned to the variable c3. Note that c3.z is initialized to null.
Then the variables c1 and c2 are both reassigned to reference the new object. So at this point, variables c1, c2, and c3 all point to the same object (the one in which z holds a null reference).
This leaves the first 2 objects without any active references, since c1 and c2 no longer point to them. These objects still contain their instance variables z that point to each other, but the objects themselves are isolated from the active part of the program. [ July 06, 2006: Message edited by: marc weber ]
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
I will suppress my every urge. But not this shameless plug: