[Solved] java this keyword inside constructor [closed]


what about anonymous classes like new Ab(2,4);

This is not an anonymous class. It’s an expression that creates a new object of type AB. The value of that expression is a reference to the object. The value of this within the AB constructor is a reference to the object. And the value of x below is a reference to the object. They’re all references to the same object.

Ab x = new Ab(4,5); // this is where my teacher confused me

in my point of view this inside constructor has no link with reference variable until constructor create the object and return the reference to variable.

The this inside the constructor has no need to refer to another reference variable. It points to the same object as x will when the constructor returns.

A reference is not the object itself. You can think it of one of possibly multiple pointers to an object.

1

solved java this keyword inside constructor [closed]