Detailed explanation of this in java
In Java, this
is a keyword that represents a reference to the current object. It can be used in the following ways:
-
A reference to the current object:
this
The keyword indicates the current object itself. In an instance method, you can usethis
to refer to the current object on which the method is called. For example,this.name
to represent the properties of the current objectname
. -
Constructor call: Inside a constructor, it can be used
this
to 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. -
Pass the current object: When you need to pass the current object as a parameter to other methods, you can use
this
the keyword. For example,doSomething(this)
means to pass the current object as a parameter todoSomething
the method. -
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
外部类名.this
to represent the current instance of the outer class. For example,OuterClass.this
represents the current object of the outer class.
In general, this
keywords 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 this
concrete code examples using keywords to demonstrate their usage:
- 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.name
used to refer to the current object name
.
- 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.name
it is used to refer to name
the instance variable of the current object so that it can be assigned the parameter value of the constructor.
- 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.
- 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.x
used 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.