Output when we call new Object() inside constructor in java





It’s a tricky program asked in interviews these days.


What will be output when we call new S() in side constructor  in java ?

package a;

class S {
    S() {
          System.out.println("in sc()");
          new S(); // will cause recursion, because whenever we call new S() ,
                              //it will recursively call constructor
    }
}

public class ConstructorRecursion {
    public static void main(String... a) {
          new S();
    }
}

This program will end up with StackOverflowError .

//output

in sc()
in sc()
in sc()
in sc()
in sc()
in sc()
in sc()
in sc()
in sc()
in sc()
Exception in thread "main" java.lang.StackOverflowError
    at java.io.FileOutputStream.write(FileOutputStream.java:326)
    at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
    at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
    at java.io.PrintStream.write(PrintStream.java:482)
    at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)
    at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:291)
    at sun.nio.cs.StreamEncoder.flushBuffer(StreamEncoder.java:104)
    at java.io.OutputStreamWriter.flushBuffer(OutputStreamWriter.java:185)
    at java.io.PrintStream.write(PrintStream.java:527)
    at java.io.PrintStream.print(PrintStream.java:669)
    at java.io.PrintStream.println(PrintStream.java:806)
    at a.S.<init>(ConstructorRecursion.java:5)
    at a.S.<init>(ConstructorRecursion.java:6)
    at a.S.<init>(ConstructorRecursion.java:6)
    at a.S.<init>(ConstructorRecursion.java:6)
    at a.S.<init>(ConstructorRecursion.java:6)
    at a.S.<init>(ConstructorRecursion.java:6)
    at a.S.<init>(ConstructorRecursion.java:6)
    at a.S.<init>(ConstructorRecursion.java:6)
    at a.S.<init>(ConstructorRecursion.java:6)
    at a.S.<init>(ConstructorRecursion.java:6)
    at a.S.<init>(ConstructorRecursion.java:6)
    at a.S.<init>(ConstructorRecursion.java:6)
    at a.S.<init>(ConstructorRecursion.java:6)
    at a.S.<init>(ConstructorRecursion.java:6)
    at a.S.<init>(ConstructorRecursion.java:6)
    at a.S.<init>(ConstructorRecursion.java:6)
    at a.S.<init>(ConstructorRecursion.java:6)
    at a.S.<init>(ConstructorRecursion.java:6)
    at a.S.<init>(ConstructorRecursion.java:6)




Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.

Related >

Constructor in java - Constructor chaining, access modifiers with constructors, constructor overloading, exception thrown, constructors are not inherited


Singleton in java, Doubly Locked Singleton class/ Lazy initialization, enum singleton, eager initialization in static block

Interface in java - Multiple inheritance, Marker interfaces, When to use interface practically, 12 features


Abstract class and abstract methods in java - When to use abstract class or interface practically, 10 features


eEdit
Must read for you :