How is the 'this' variable in Java actually set to the current object?

前端 未结 5 1934
囚心锁ツ
囚心锁ツ 2021-02-03 21:19

Consider:

class TestParent{
  public int i = 100;
  public void printName(){
    System.err.println(this); //{TestChild@428} according to the Debugger.
    Syste         


        
5条回答
  •  再見小時候
    2021-02-03 21:49

    Well when a new object is created that object has an address in memory so you can think of it as if the object had a private member this that is set to the address when the object is created. You can also think of it like this: obj.method(param) is just syntactic sugar for method(obj, param); and this is actually a parameter of method.

提交回复
热议问题