Win a copy of Fixing your Scrum this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Circular Reference...

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Folks,

Can you help me to understand the circular reference?
From the following code where actually circular reference happening.

Thanks, Raghu.K

class MyClass{
private MyClass z;
public void other(MyClass c){z = c;}
protected void finalize(){System.out.prinln("called');}
}
class Test{
private static void f(){
}

public static void main(String[] args){
MyClass c1=new MyClass();
MyClass c2=new MyClass();
c1.other(c2);
c2.other(c1);
MyClass c3 = new MyClass();
c1=c3;
c2=c3;
f();
}
}
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

c1.other(c2);
c2.other(c1);

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.

c1=c3;
c2=c3;

For your reference .. Circular reference is nothing but two objects having reference of each other.

Pls correct if i am wrong.
 
HungryJavaGoat
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A small correction...the defintion i provided for circular reference is just an example ..actually it can involve any number of objects ..for example...

A - > B - > C -> D - > A

Above is also a circular reference where A have reference to B , B have reference to C , C have reference to D, and again D have reference to A which forms a circle....
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"karthik,"

Welcome to JavaRanch!

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.

You can edit your display name here.

Thank you for your prompt attention, and enjoy the ranch!

-Marc
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
mooooooo ..... tiny ad ....
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic