Home  >  Article  >  Java  >  What does this refer to in java

What does this refer to in java

angryTom
angryTomOriginal
2019-11-11 15:36:237501browse

What does this refer to in java

What does this refer to in java

Using the this keyword in the class method definition means using the A reference to the method's object.

Use this when you must indicate who is currently using the method.

Sometimes this can be used to deal with the situation where member variables and parameters in methods have the same names.

This can be regarded as a variable, its value is a reference to the current object, It points to the object of itself. (Recommended tutorial: java tutorial)

public class Leaf {
    int i = 0;
    public Leaf(int i) {
        this.i = i;
    }
    Leaf increament() {
        i++;
        return this;
    }
    void print() {
        System.out.println("i = " + i);
    }
    public static void main(String[] args) {
        Leaf leaf = new Leaf(100);
        leaf.increament().increament().print();
    }
}

What does this refer to in java

The above is the detailed content of What does this refer to in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn