0

I am very new to programming trying to learn it all by myself. I am facing an issue that I can not resolve and I couldn't find any good answers online.

I have a series of classes implementing an interface, one of which needs to provide a reference to any of the other types of objects. I post a simplified version of my code below, I know it might not be all correct but I hope you can understand it.

What my problem is that I am not able to write a method that gets rid of these kind of circular references like in the main method I posted below. I read a lot of things, about graph and recursive algorithms, but I couldn't find out an answer yet. Any help would be appreciated. Thank you!

public class ReferenceObject implements A {

    A reference;

    public void setReference(A reference){
        this.reference=reference;
    }

    public Object getValue(){
        return reference.getValue;
    }

}

public static void main(String[] args) {
    ReferenceObject r1 = new ReferenceObject();
    ReferenceObject r2 = new ReferenceObject();
    ReferenceObject r3 = new ReferenceObject();
    r1.setReference(r2);
    r2.setReference(r3);
    r3.setReference(r1);
}

2 Answers2

0

Your case is fairly simple:

public class ReferenceObject implements A {

  A reference;

  public void setReference(A reference) {
    this.checkForCircularRef(reference);
    this.reference=reference;
  }

  public Object getValue(){
    return reference.getValue;
  }

  private void checkForCircularRef(A reference) {
    if(reference != null) {
      A nextRef = reference;

      while(nextRef != null) {
        if(nextRef.equals(this)) {
          throw new IllegalArgumentException("Reference refers back to this object!");
        }

        nextRef = reference.reference;
      }
    }
  }

}

The checkForCircularRef method essentially does a linear search for a reference to the the calling object (this). More complicated structures might need to a more complex search.

If your objects have pre-existing circular references, you will probably need to keep the references in a data structure, and check that you have not seen each reference you encounter previously, or the code I provided will get stuck in an infinte loop.

Adam
  • 2,214
  • 1
  • 15
  • 26
0

I have a series of classes implementing an interface, one of which needs to provide a reference to any of the other types of objects. I post a simplified version of my code below, I know it might not be all correct but I hope you can understand it.

What my problem is that I am not able to write a method that gets rid of these kind of circular references like in the main method I posted below.

It is a bit highlighted what needs attention imo.

The first thing is that the Java Garbage collector it will collect the reachable objects, and it will free those references. The process is very similar, when you try to compile a project. In most of the cases all source files are compiled. But there are the few exceptions, same with the garbage collection.

Please take a look at this links:

How does Java Garbage Collection work with Circular References?

How does Java garbage collector deals with circular references when their access path is broken?

I do really hope you don't want to fix the Java Garbage collector algorithm, because it isn't a beginner task.

My suggestion it is: modify your code until you are fine with the Java garbage collector automatic circular reference free system.

Community
  • 1
  • 1
matheszabi
  • 594
  • 5
  • 16