Home >Java >javaTutorial >What should you pay attention to when using this keyword in Java?
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; } }
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!