Home > Java > javaTutorial > What should you pay attention to when using this keyword in Java?

What should you pay attention to when using this keyword in Java?

PHPz
Release: 2023-04-20 16:46:08
forward
899 people have browsed it

Explanation

Non-static methods need to be accessed by objects. Which object calls this method, this is the object.

Usage Notes

1. In the program, the representative refers to the current object, and the this keyword needs to be used in some non-static method.

2. This keyword can be omitted.

3. You can omit this when accessing non-static members in the current category.

Example

This cannot be omitted:

public class Person {
String name;
int age;
char gender;
void setInfo(String name, int age, char gender) {
// 就近原则
this.name = name;
this.age = age;
this.gender = gender;
}
}
Copy after login

The above is the detailed content of What should you pay attention to when using this keyword in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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