What is the meaning of “this” in Java?

后端 未结 21 2569
走了就别回头了
走了就别回头了 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 05:59

    As everyone said, this represents the current object / current instance. I understand it this way, if its just "this" - it returns class object, in below ex: Dog if it has this.something, something is a method in that class or a variable

    class Dog {
    private String breed;
    private String name;
    
    Dog(String breed, String name) {
        this.breed = breed;
        this.name = name;
    }
    
    public Dog getDog() {
        // return Dog type
        return this;
    }
    

    }

提交回复
热议问题