Detailed explanation of this in java

Detailed explanation of this in java

In Java, thisis a keyword that represents a reference to the current object. It can be used in the following ways:

  1. A reference to the current object: thisThe keyword indicates the current object itself. In an instance method, you can use thisto refer to the current object on which the method is called. For example, this.nameto represent the properties of the current object name.

  2. Constructor call: Inside a constructor, it can be used thisto call other constructors in the same class. In this way, mutual calls between constructors can be realized. For example, this(...)it means calling other constructors in the same class.

  3. Pass the current object: When you need to pass the current object as a parameter to other methods, you can use thisthe keyword. For example, doSomething(this)means to pass the current object as a parameter to doSomethingthe method.

  4. Refer to the outer class object in the inner class: In the inner class, if you need to refer to the outer class object, you can use it 外部类名.thisto represent the current instance of the outer class. For example, OuterClass.thisrepresents the current object of the outer class.

In general, thiskeywords are used to refer to the current object and can be used in scenarios such as instance methods, constructors, method parameters, and inner classes. It provides a convenient way to access the properties and methods of the current object and perform related operations.

code example

When using the keyword in Java this, it represents the reference of the current object. Here are a few thisconcrete code examples using keywords to demonstrate their usage:

  1. Access instance variables and methods:
public class Person {
    
    
    private String name;

    public void setName(String name) {
    
    
        this.name = name; // 使用this关键字引用当前对象的name实例变量
    }

    public void sayHello() {
    
    
        System.out.println("Hello, my name is " + this.name); // 使用this关键字引用当前对象的name实例变量
    }
}

In the above example, an instance variable this.nameused to refer to the current object name.

  1. Distinguish between instance variables and parameter variables in constructors:
public class Person {
    
    
    private String name;

    public Person(String name) {
    
    
        this.name = name; // 使用this关键字引用当前对象的name实例变量,并将参数值赋给它
    }
}

In the above example, this.nameit is used to refer to namethe instance variable of the current object so that it can be assigned the parameter value of the constructor.

  1. Call other constructors:
public class Person {
    
    
    private String name;
    private int age;

    public Person(String name) {
    
    
        this(name, 0); // 使用this关键字调用同一类中的其他构造函数
    }

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

In the above example, this(name, 0)it is used to call another constructor in a constructor and pass the corresponding parameters.

  1. Reference the outer class object in the inner class:
public class OuterClass {
    
    
    private int x;

    public class InnerClass {
    
    
        public void printX() {
    
    
            System.out.println("x = " + OuterClass.this.x); // 使用外部类名.this来引用外部类对象
        }
    }
}

In the above example, the instance variable OuterClass.this.xused to refer to the current object of the outer class x.

These examples show the use of keywords in different situations this, which can be used to refer to instance variables of the current object, call other constructors, refer to outer class objects in inner classes, etc.

Guess you like

Origin blog.csdn.net/a772304419/article/details/130979729