What is the meaning of “this” in Java?

后端 未结 21 2643
走了就别回头了
走了就别回头了 2020-11-21 05:41

Normally, I use this in constructors only.

I understand that it is used to identify the parameter variable (by using this.something), if i

21条回答
  •  面向向阳花
    2020-11-21 06:03

    It refers to the instance on which the method is called

    class A {
    
      public boolean is(Object o) {
        return o == this;
      }
    
    }
    
    A someA = new A();
    A anotherA = new A();
    someA.is(someA); // returns true
    someA.is(anotherA); // returns false
    

提交回复
热议问题