how instance of class is creating recursively and thus causing stackoverflow error
<p>Consider following 2 programs giving same error<br>
First calss:</p>

<pre><code>public class Testing {    
    Testing t=new Testing();        
    public static void main(String args[]){   
        testing t1=new testing();  
    }
}
</code></pre>

<p>Second class:</p>

<pre><code>class foo{
    void baz(){
        new testing();
    }
}
public class testing {
    testing t=new testing();    
    public static void main(String args[]){
    foo f=new foo();
        f.baz();
    }       
}
</code></pre>

<p>how does above code giving following error?<br>
 I know instance of class is creating recursively but I want to know how?</p>

<pre><code>Exception in thread "main" java.lang.StackOverflowError
    at com.Testing.&lt;init&gt;(Testing.java:4)
    at com.Testing.&lt;init&gt;(Testing.java:4)
</code></pre>

<p>also Why doesn't this happen if we do</p>

<pre><code>    public class testing { 
    testing t2=new testing(); 
    testing t1=new testing(); 
    public static void main(String args[])
    {//anything}
    }
</code></pre>

<p>as t1 will require t2 object to be initalized and vice-versa?</p>
