Please suggest me to solve the below infinite loop . Class object contains the collection of same type objects. While converting to String , The object calls the toString of each object in the collection. Hence it leads to infinite loop. Please dont use any static variables.
import java.util.LinkedList;
/**
*
* @author ranga
*/
public class MyList {
LinkedList<Object> l1,l2;
MyList() {
l1 = new LinkedList<Object>();
l2 = new LinkedList<Object>();
l2.add(l1);
l1.add(l2);
}
@Override
public String toString() {
return l1.toString();
}
public static void main(String ...args) {
MyList m = new MyList();
System.out.println(m);
}
}