I have 2 code snippets
Snippet 1
class Test7
{
Test6this t;
Test7(Test6this t)
{
System.out.println("entering");
this.t=t;
System.out.println(t);
}
}
public class Test6this {
Test6this()
{
Test7 t7=new Test7(this);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Test6this t=new Test6this();
System.out.println("main:"+t);
}
}
Snippet 2
class Test7
{
Test6this t;
Test7(Test6this t)
{
this.t=t;
System.out.println(t);
}
}
public class Test6this extends Test7{
Test6this()
{
super(this);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Test6this t=new Test6this();
System.out.println("main:"+t);
}
}
First one is working but second one gives error Cannot refer to 'this' nor 'super' while explicitly invoking a constructor
But in both the cases we are using this in Test6this class so call the constructor of Test7 before object of Test6this is created then how snippet 1 is workign and snippet 2 not
Please help me on this. Thanks in advance