Home > Java > Javagetting Started > What does this in java mean?

What does this in java mean?

(*-*)浩
Release: 2019-11-13 11:26:06
Original
3812 people have browsed it

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!

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