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

前端 未结 5 1929
囚心锁ツ
囚心锁ツ 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 22:04

    What's happening here is that there are two completely different fields both called i; to use their full names, one is TestParent::i and one is TestChild::i.

    Because the method printName is defined in TestParent, when it refers to i, it can only see TestParent::i, which is set to 100.

    Whereas when you set i to 200 in TestChild, both fields called i are visible, but because they have the same name, TestChild::i hides TestParent::i, and you end up setting TestChild::i and leaving TestParent::i untouched.

提交回复
热议问题