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

Comments

Popular posts from this blog

maven fortify plugin : Unable to load build session with ID XXXXX .. See log file for more details -

c# - Primavera WebServices does not return any data -

android - Display emoji panel with genymotion - keyboard/touch input? -