Home > Java > Javagetting Started > Three uses of this keyword in java

Three uses of this keyword in java

王林
Release: 2020-02-20 18:05:00
forward
2666 people have browsed it

Three uses of this keyword in java

This is an object of its own, representing the object itself. It can be understood as: a pointer to the object itself.

The usage of this in Java can be roughly divided into three types:

1. Ordinary direct reference, this is equivalent to pointing to the current object itself.

(Recommended tutorial: java introductory tutorial)

2. The names of participating members have the same name, use this to distinguish them:

public Person(String name, int age) {
    this.name = name;
    this.age = age;
}
Copy after login

3. Quote The constructor of this class

class Person{
    private String name;
    private int age;
    
    public Person() {
    }
 
    public Person(String name) {
        this.name = name;
    }
    public Person(String name, int age) {
        this(name);
        this.age = age;
    }
}
Copy after login

For more programming-related content, please pay attention to the Programming Introduction column on the php Chinese website!

The above is the detailed content of Three uses of this keyword in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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