The use of the Java this keyword is related to method overloading

  • 2020-04-01 04:10:13
  • OfStack

Detailed explanation of the Java this keyword
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 the object can be called. Such as:


public class Demo{
  public int x = 10;
  public int y = 15;
  public void sum(){
    //Take the member variable through 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 results:


x + y = 25

In the above program, obj is an instance of Demo class, this is equivalent to obj, perform int z = this.x + this.y; , is the same as executing int z = obj.x + obj.y; .

Note: this only makes sense after the class is instantiated.
Use this to distinguish between variables with the same name

What if you want to call a member variable inside a method when it is renamed with a variable inside the method? You can only use this, 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 + " That's already true " + age + " years ");
  }
 
  public static void main(String[] args) {
    Demo obj = new Demo(" The school ", 3);
    obj.say();
  }
}

Operation results:


 The name of the website is micro academy, which has been established 3 years 

The scope of the parameter is the whole method body and is a local variable. In Demo(), the parameter and the member variable have the same name. If you do not use this, you will access the local variables name and age instead of the member variable. In say(), we didn't use this because the scope of the member variable is the entire instance. Of course, we can also add this:


public void say(){
  System.out.println(" The name of the website is " + this.name + " That's already true " + this.age + " years ");
} 


By default, Java associates all member variables and member methods with this, so using this is redundant in some cases.
Initializes the object as a method name

This is equivalent to calling the other constructor of the class, which must be the first sentence of the constructor. Here's an example:


public class Demo{
  public String name;
  public int age;
  
  public Demo(){
    this(" The school ", 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 + " That's already true " + age + " years ");
  }
 
  public static void main(String[] args) {
    Demo obj = new Demo();
    obj.say();
  }
}

Operation results:
The name of the website is micro-academy, which has been established for 3 years

It is worth noting that:
To invoke another constructor in a constructor, the invoke action must be placed at the beginning.
A constructor cannot be invoked in any method other than a constructor.
Only one constructor can be called within a constructor.

The code above involves method overloading, where Java allows multiple methods with the same name to appear, as long as the parameters are different. We'll talk about that in subsequent chapters.
Pass as a parameter

When you need to call a method in some completely separate class and pass a reference to the current object as an argument. Such as:


public class Demo{
  public static void main(String[] args){
    B b = new B(new A());
  }
}
class A{
  public A(){
    new B(this).print(); //Anonymous objects
  }
  public void print(){
    System.out.println("Hello from A!");
  }
}
class B{
  A a;
  public B(A a){
    this.a = a;
  }
  public void print() {
    a.print();
    System.out.println("Hello from B!");
  }
}

Operation results:


Hello from A!
Hello from B!

An anonymous object is one that has no name. If the object is used only once, it can be used as an anonymous object. (new B(this)).print(); , create a nameless object with new B(this), and then call its methods.

Java method overloading
In Java, multiple methods in the same class can have the same name as long as they have different parameter lists, which is called method overloading.

Parameter list is also called parameter signature, including the type of parameters, the number of parameters and the order of parameters, as long as there is a difference is called parameter list is different.

Overloading is a fundamental feature of object orientation.

Let's look at a detailed example.


public class Demo{
  //A common method with no arguments
  void test(){
    System.out.println("No parameters");
  }
  //Overloads the above method with an integer argument
  void test(int a){
    System.out.println("a: " + a);
  }
  //Overloads the above method with two arguments
  void test(int a,int b){
    System.out.println("a and b: " + a + " " + b);
  }
  //Overloads the above method with a double - precision argument
  double test(double a){
    System.out.println("double a: " + a);
    return a*a;
  }
  
  public static void main(String args[]){
    Demo obj= new Demo();
    obj.test();
    obj.test(2);
    obj.test(2,3);
    obj.test(2.0);
  }
}

Operation results:


No parameters
a: 2
a and b: 2 3
double a: 2.0

From the above example, you can see that an overload is a function in a class that has the same function name but a different parameter. The result of overloading is to let a program segment minimize the variety of code and methods.

Description:

Different parameter list includes: different number, type and order. It is not ok to just have different parameter variable names. Like member methods, constructors can be overloaded. Methods declared final cannot be overloaded. Methods declared static cannot be overridden, but can be declared again.

Rules for method overloading:

Method names must be the same. The parameter list must be different (different number, or type, order, etc.). The return type of the method can be the same or different. Simply returning a different type is not enough to be an overload of a method.

Method overload:
When the method name is the same, the compiler will match the method one by one according to the number of parameters, parameter type, etc., to select the corresponding method. If the match fails, the compiler will report an error, which is called overload resolution.


Related articles: