Home  >  Article  >  Java  >  What does this in java mean?

What does this in java mean?

(*-*)浩
(*-*)浩Original
2019-11-13 11:26:063703browse

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

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:
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