• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Circular Reference in Java

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

I was trying to understand "Circular references" which says one object has a reference to another and the second object has a reference to the first
but i am not able to understand this.

Then there is a code which says it has circular reference but i think it doesn't (or may be i do not understand). The code is



I tried drawing a figure which is below
Now after only object 1 is available for GC, but i do not see any circular reference.
Thanks



Filename: untitled.bmp
Description: Figure as per code
File size: 788 Kbytes
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That code does not show a circular reference. By the way, have you tried compiling and running it? It will never reach line 24, and it will crash after a while in line 23. That's because the following happens:

  • Line 23: You create a new a
  • Line 6: In the constructor of a, a new b is created
  • Line 15: In the constructor of b, a new a is created
  • Line 6: In the constructor of a, a new b is created
  • Line 15: In the constructor of b, a new a is created
  • Line 6: In the constructor of a, a new b is created
  • Line 15: In the constructor of b, a new a is created
  • Etcetera until the stack overflows

  • A circular reference happens when one object refers to another, and that other one refers to the first object. For example:

    Java's garbage collector is smart enough to clean up the objects if there are circular references, but there are no live threads that have any references to the objects anymore. So having a circular reference like this does not create a memory leak.
     
    Rameshwar Soni
    Ranch Hand
    Posts: 247
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Jasper !

    As per your code, firstly in the main() method you have created two references variable "one" and "two" which are pointing to the object of "A" and "B" respectively.

    Then you have invoked the method setB() and you are passing the reference variable "two" which is pointing to Object of class B

    to the reference variable "b" which is also of type B (class B)

    So "two" and "b" both are pointing to Class B object

    so how come there is a circular reference.?

    Same is done with the reference variable "one"

     
    Bartender
    Posts: 4568
    9
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Rameshwar Soni wrote:So "two" and "b" both are pointing to Class B object

    so how come there is a circular reference.?


    Because that's what a circular reference is! You have a circular reference if you can keep following references and end up back where you started.

    In Jesper's example: start at one. one.b is the B object. one.b.a is the same A object you started with (so one.b.a == one). That's circular.

    It can be more complicated that that - you can have more than two classes involved. If you could arrange a bunch of classes and objects so that one.b.c.d.e.f.g.a == one, then you'd have a circular reference.
     
    Bartender
    Posts: 1111
    Eclipse IDE Oracle VI Editor
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    so a circular reference is about variables being circular... not classes being circular.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic