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

What does this refer to in java

angryTom
Release: 2019-11-11 15:36:23
Original
7595 people have browsed it

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();
    }
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template