The This keyword is used to represent the current object itself, or an instance of the current class, through which all methods and properties of this object can be called. For example:
- Public class Demo{
- public int x = ten;
- public int y = ;
- public void sum(){
- To take member variables by this point
- int Z = this. x + this. Y;
- System. Out. println("x + y =" + z);
- }
- Public static void main(String[] args) {
- Demo obj = New demo();
- Obj. Sum();
- }
- }
Operation Result:
X + y = 25
In the above program, obj is an instance of the Demo class, which is equivalent to obj, which executes int z = this.x + this.y, which is equivalent to executing int z = obj.x + obj.y;.
Note: This only makes sense if the class is instantiated.
Use this to distinguish a variable with the same name
How do you want to call member variables inside a method when the member variable has the same name as the variable inside the method? This can only be used at this time, for example:
- Public class Demo{
- public String name;
- public int age;
- public Demo(String name, int age){
- this. Name = name;
- this. Age = Age;
- }
- public void say(){
- System. Out. println("The name of the website is" + name + ", has been established" + Age + "Year");
- }
- public static void main(String[] args) {
- demo obj = New demo("Micro School", 3);
- Obj. Say();
- }
- }
Operation Result:
The name of the site is the Micro School, has been set up for 3 years
The scope of the formal parameter is the entire method body, which is a local variable. In the demo (), the names of the parameters and member variables are the same, and if you do not use this, the local variables name and age, not the member variables, are accessed. In Say (), we do not use this because the scope of the member variable is the entire instance, and of course it can be added as well:
- Public void say(){
- System. Out. println("The name of the website is" + this . Name + ", has been set up" + this . Age + "Year" );
- }
Java By default associates all member variables and member methods with this, so using this is redundant in some cases.
Initialize an object as a method name
This is equivalent to invoking the other constructor method of this class, which must be the first sentence of the constructor method. Examples are as follows:
- Public class Demo{
- public String name;
- public int age;
- public Demo(){
- this("micro-learning Court", 3);
- }
- public Demo(String name, int age){
- this. Name = name;
- this. Age = Age;
- }
- public void say(){
- System. Out. println("The name of the website is" + name + ", has been established" + Age + "Year");
- }
- public static void main(String[] args) {
- demo obj = New demo();
- Obj. Say();
- }
- }
Operation Result:
The name of the site is the Micro School, has been set up for 3 years
It is worth noting that:
- Invoking another constructor method in the constructor method, the invocation action must be placed at the starting position.
- You cannot call a construction method within any method other than the constructor method.
- Only one constructor method can be called within a constructor method.
The above code involves method overloading, where Java allows multiple methods with the same name, as long as the parameters are different. A follow-up section will explain.
Pass as parameter
You need to call a method in some completely detached class and pass a reference to the current object as a parameter. For example:
- Public class Demo{
- public static void main(String[] args){
- b B = new b(new A());
- }
- }
- Class A{
- public A(){
- New B(this). Print(); //Anonymous object
- }
- public void print(){
- System. Out. println("Hello from a!" );
- }
- }
- Class B{
- a ;
- public B(a a){
- this. A = a;
- }
- public void print() {
- A. Print();
- System. Out. println("Hello from b!" );
- }
- }
Operation Result:
Hello from A!
Hello from B!
An anonymous object is an object without a name. If the object is used only once, it can be used as an anonymous object, in code new B (this). Print (); Equivalent to (new B (this)). Print (), first create an object without a name by using new B (this), and then call its method.
8.Java this keyword