I have the following code and its throwing StackOverflowException because of nested constructor call, and I know memory gets allocated only when constructor runs successfully.
package interview;
public class StackOverflow {
int i;
StackOverflow()
{
System.out.println(" i= "+i);
System.out.println("in Constructor");
StackOverflow sOf = new StackOverflow();
sOf.i=5;
System.out.println(" i= "+i);
}
public static void main(String[] args) {
System.out.println("in main");
StackOverflow s = new StackOverflow();
s.i=10;
System.out.println(" i= "+s.i);
}
}
So my doubt here is what is happening to the value of 'i' ? Is it getting stored somewhere in stack or heap? In which case the above code can throw the following Exception?
OutOfMemoryException