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!