java - What happens when 2 classes create objects of each other? -
java - What happens when 2 classes create objects of each other? - as far know when object created within given class memory allocated particular object. in case: public class kinda { kindb b = new kindb(); // rest of code } public class kindb { kinda = new kinda(); // rest of code } what happens in case on memory allocation side of things when 2 classes create objects of eachother? you'll stackoverflowerror if seek initialize 1 of classes, since initializer of kinda creates new object of type kindb , hence initializer of kindb runs, creates new object of type kinda , kinda 's initializer runs again, creates , initializes new object of type kindb , on ... this infinite recursion, hence stackoverflow. java