This is used inside a class to represent the class instance itself.
This keyword is a reference to itself inside the class, which can facilitate methods in the class to access its own properties.
Usage of this in java
1. When local variables and member variables have the same name, use this in the method. member variables to distinguish.
class Demo{ String str = "这是成员变量"; void fun(String str){ System.out.println(str); System.out.println(this.str); this.str = str; System.out.println(this.str); } } public class This{ public static void main(String args[]){ Demo demo = new Demo(); demo.fun("这是局部变量"); } }
Analysis: The above class Demo has a member variable str and a local variable str (formal parameter in the class method). It is obvious that the local variable and the member variable have the same name. At this time, usually Direct use of str in the method actually uses the local variable str, which has no effect on the member variable str. At this time, if you need to do something with the member variable, you must use the this keyword.
There is a question. If there is no str in the method, what will happen if the member variable str is used in the method? In fact, all operations within the method are performed on the member variable str. There is a sentence in the middle of page 84 of Java Programming Thoughts: If you call another method of the same class inside a method, you do not need to use this. Similarly, if there are no local variables and member variables with the same name in a method, you do not need to use this when using member variables in this method. You can run the following code to see.
class Demo{ String str = "这是成员变量"; void fun(String str1){ System.out.println(str1); System.out.println(str); } } public class This{ public static void main(String args[]){ Demo demo = new Demo(); demo.fun("这是局部变量"); } }
2. This keyword passes the current object to other methods
There is a very classic example here, which is the example on page 85 of Java Programming Thoughts. Let's take it out and study it carefully.
class Person{ public void eat(Apple apple){ Apple peeled = apple.getPeeled(); System.out.println("Yummy"); } } class Peeler{ static Apple peel(Apple apple){ //....remove peel return apple; } } class Apple{ Apple getPeeled(){ return Peeler.peel(this); } } public class This{ public static void main(String args[]){ new Person().eat(new Apple()); } }
This is my own understanding. It may not be correct. See what it says in the book: Apple needs to call the Peeler.peel() method, which is an external Utility method that will perform an operation that for some reason has to be placed outside of Apple (perhaps because that external method is going to be applied to many different classes and you don't want to duplicate the code). In order to pass itself to an external method, the this keyword must be used.
Analysis: Imagine a scenario. If the work of peeling various fruits is the same, as long as you give me the fruit, I will peel it in the same way. Then combined with the above example, a fruit is passed in. Before we eat getPeeled(), we must pass this fruit as a parameter to the external peel(), and use this to represent itself and pass it to the external method.
3. When you need to return a reference to the current object, you often write return this in the method;
The advantage of this approach is: when you use an object to call This method returns the modified object and can use the object to perform other operations. Therefore it is easy to perform multiple operations on an object.
public class This{ int i = 0; This increment(){ i += 2; return this; } void print(){ System.out.println("i = " + i); } public static void main(String args[]){ This x = new This(); x.increment().increment().print(); } }
The result is:
4
4. When calling the constructor in the constructor, you need to use this
A class has many constructors, sometimes If you want to call other constructors in one constructor to avoid code duplication, you can use the this keyword. There is a saying in Java programming thinking: Usually when writing this, it refers to "this object" or "current object", and it itself represents a reference to the current object. In the constructor, if a parameter list is added to this, it has a different meaning. This results in an explicit call to a constructor that matches this argument list; this provides a direct path to calling other constructors.
Careful analysis:
Starting from the main function, new Flower() will allocate space in the memory and initialize the object. The initialized object is to call the constructor, here No parameters are written, of course the default constructor is called, which is the parameterless constructor.
The first line of code of this parameterless constructor is this("hi",122); what this means is that the parameterless constructor calls the constructor with two parameters and comes to the For a two-parameter constructor, the first line of code is this(s); this line of code automatically matches the constructor with one parameter, and it is found that Flower(String ss) matches, both of which are String type parameters.
Then the constructor with a String type parameter is called, printing: Constructor with only String type parameter s = hi
; and then returns to the previous level to call the function, that is, with A constructor with two parameters, prints: Constructor with parameters of type String and int
; Go back to the previous level, which is a constructor without parameters, prints: Default constructor
.
At this time, the constructor has initialized the newly created object, and finally printed in the last line of code of the main function: petalCount=122 s=hi
.
Draw a picture to see more clearly.
There are a few points to note:
1, this can only call one constructor, and cannot call two at the same time in one constructor Constructor;
2, the constructor you call must be placed at the beginning. This also explains why you cannot call two constructors in one constructor, then the second one must not be at the beginning.
3. In this example, in the constructor with two parameters, you can use this to call any other constructor with only one parameter. It is up to you. You can call any one.
4. This cannot be used to call the constructor in a method other than the constructor. The comments in the code cannot be compiled correctly.
This summary
1. Indicates a reference to the current object!
2. Indicates using class member variables instead of function parameters.
3. Used to reference the constructor that satisfies the specified parameter type in the constructor (actually, it is also the constructor). But you must be very careful here: only one constructor can be referenced and it must be at the beginning!
4. Obviously this cannot be used in static methods, because this refers to the current object, and static has no object.
php Chinese website, a large number of free Java introductory tutorials, welcome to learn online!
The above is the detailed content of What does this mean in java?. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version
SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version
Useful JavaScript development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
