-3

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

Ankit
  • 271
  • 3
  • 11
  • 1
    Isn't the error message very clear about this? `new Test7(this);` works in a constructor, `super(this);` does not. – f1sh Mar 22 '22 at 11:14
  • 2
    In essence, any parameters you are passing to `super` would logically be evaluated before the `super()` call is made. You can't call `this` before that happens. The first snippet is akin to silently doing `super()`, then the `new Test7(this);` – Rogue Mar 22 '22 at 11:16

1 Answers1

1

An explicit constructor invocation is when you use this(...) or super(...) inside a constructor to relegate the construction to another constructor or to a constructor of the parent class, as defined in the JLS item 8.8.7.1.

If you read further down that item, you'll see that:

An explicit constructor invocation statement in a constructor body may not refer to any instance variables or instance methods or inner classes declared in this class or any superclass, or use this or super in any expression; otherwise, a compile-time error occurs.

Basically, when you are still constructing the object, you can't use it or any of its members as a parameter to another constructor of the same object.

It's basically an attempt to circularly build an object while it's already being built.

The difference from your first snippet is that in your first snipped, the Test7 instant is a different object, allocated somewhere else. You are passing a partially built Test6This to it, but there is no self reference her.

In your second snippet, the Test7 object is the same object that is being built. It's not a separate instance, it's just a different view of the same object. So you are passing it itself. This is prevented by the above rule.

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79