Home Java Javagetting Started What does this in java mean?

What does this in java mean?

Nov 13, 2019 am 11:26 AM
java

What does this in java mean?

this represents the current object. this calls the attributes in this class, that is, the member variables in the class; this calls other methods in this class;

this To call other constructors in this class, the call must be placed in the first line of the constructor. (Recommended study: java course)

This this must be used in a method of a certain class.

For example:

public class persion{
    private String name=null;
    private int age;
    public void setName(String name){
        this.name=name;
    }
    public String getName(){
        return name;
    }
}
Copy after login

In this class, the first variable in the setName method uses this to distinguish it from the parameter this of this method. Indicates that this name refers to the name attribute of the Person class, not the name parameter. If this is removed, after executing this method, the name attribute of the Person class will still be null

returned by the getName method The name of the Person class is not used because it is not necessary, because the compiler will know that this name refers to the name of the Person and not other names (because there is no name variable to find a way to confuse it). Of course, you can also explicitly use return this.name in this method, that’s right.

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

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

Square Root in Java

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Perfect Number in Java

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Random Number Generator in Java

Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

Armstrong Number in Java

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Weka in Java

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Smith Number in Java

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

Java Spring Interview Questions

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Break or return from Java 8 stream forEach?

See all articles