I have two classes, Class A and Class B.
Class A has an ArrayList of objects Class B.
Each Class B object has an attribute that stores a Class A object.
So when I create a Class A object, it populates the ArrayList of objects Class B. Each object Class B creates an object Class A (same as the one that is being populated), which creates an ArrayList of objects Class B… and so on.
What should I do to avoid this infinite recursion?
This should fix infinite recursion: (Java assumed)
Note that
B‘sattributefield is not assigned usingnewbut by passing in a reference toA. By not usingnewwe do not create a new instance of theAclass and avoid recursion.