search
HomeJavaJavaBaseWhen to use this keyword in java

When to use this keyword in java

Nov 18, 2019 pm 02:47 PM
javathisuseKeywords

When to use this keyword 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("这是局部变量");
    }
}

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

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

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 keyword in java

Recommended tutorial: Java tutorial

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version