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);
}