Home > Java > javaTutorial > body text

Usage of this in java

下次还敢
Release: 2024-04-29 02:12:16
Original
692 people have browsed it

This keyword is a reference pointing to the object being executed. Main uses include: accessing instance variables and methods. Constructors refer to other constructors. Access external class from inner class. Access the nested class itself within a nested class. Access external classes from anonymous inner classes.

Usage of this in java

Usage of this keyword in Java

What is this keyword?

This keyword is a reference that points to the object being executed.

Purpose of this keyword

This keyword is mainly used for the following purposes:

  • Access instance variables and methods: You can use the this keyword to access instance variables and methods declared in the class.
  • Reference to other constructors in the constructor: You can use the this keyword in the constructor to call other constructors with different parameters.
  • Accessing external classes in internal classes: In internal classes, you can use the this keyword to access members of external classes.
  • Accessing the nested class itself in a nested class: In a nested class, you can use the this keyword to refer to the nested class itself.
  • Accessing external classes in anonymous inner classes: In anonymous inner classes, you can use the this keyword to access members of the class containing the anonymous inner class.

Example of this keyword

<code class="java">class Person {
    private String name;

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }
}</code>
Copy after login

In this example:

  • Using this key in the constructor Words: In the constructor Person(String name), the this.name = name statement is used to assign the value of the name parameter to the instance variable name.
  • Use the this keyword in the method: In the getName() method, the this.name statement is used to return the value of the name instance variable.

Notes on this keyword

  • this keyword must be used in a non-static context: In a static method The this keyword cannot be used in or blocks.
  • The this keyword is an implicit parameter: For non-static methods, the compiler will automatically pass the this object as the first parameter to the method. Therefore, the this keyword can be omitted in methods.

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

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!