Home > Java > javaTutorial > When to use this in java?

When to use this in java?

青灯夜游
Release: 2019-11-18 17:33:48
Original
3309 people have browsed it

This only exists inside the method and is used to represent the object that calls the method. It can be understood that there is a local variable called this inside each method. Whenever an object is initialized, the address of the object is passed to the this variable in each method of the object, so that this object can be used inside the method. .

When to use this in java?

#When to use this in java?

#1. When local variables and member variables have the same name, use this to represent the member variable in the method to distinguish them

Example:

class Demo{
    String str = "这是成员变量";
    void fun(String str){
        System.out.println(str);
        System.out.println(this.str);
        this.str = str;
        System.out.println(this.str);
    }
}
public class This{
    public static void main(String args[]){
        Demo demo = new Demo();
        demo.fun("这是局部变量");
    }
}
Copy after login

2. This keyword passes the current object to other methods

Example:

class Person{
    public void eat(Apple apple){
        Apple peeled = apple.getPeeled();
        System.out.println("Yummy");
    }
}
class Peeler{
    static Apple peel(Apple apple){
        //....remove peel
        return apple;
    }
}
class Apple{
    Apple getPeeled(){
        return Peeler.peel(this);
    }
}
public class This{
    public static void main(String args[]){
        new Person().eat(new Apple());
    }
}
Copy after login

3. When you need to return a reference to the current object, you often write return this

in the method. The advantage of this approach is that when you use an object to call this method, the method returns the modified object, and you can use the object to do other operations. Therefore it is easy to perform multiple operations on an object.

public class This{
    int i = 0;
    This increment(){
        i += 2;
        return this;
    }
    void print(){
        System.out.println("i = " + i);
    }
    public static void main(String args[]){
        This x = new This();
        x.increment().increment().print();
    }
}
结果为:4
Copy after login

4. To call the constructor in the constructor, you need to use this

A class has many constructors. Sometimes you want to use a constructor in a constructor. To call other constructors in a function to avoid code duplication, you can use the this keyword.

When to use this in java?

Recommended tutorial: Java tutorial

The above is the detailed content of When to use this 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