Four uses of this keyword in Java (with code examples)

Keywords: Attribute

this keyword can only be used inside a method

This has four uses: calling member variables, calling member methods, calling other overloaded constructors, and representing the current object.It is important to understand the use of this, and these four uses are described in detail below.

1. this calls member variables

Calling a member variable with this occurs inside a method, and when the name of a member variable (a variable declared under a class) is the same as the name of a local variable (a variable declared within a method), the name of this. variable is used to indicate that the variable represents a member variable.

//Declare member variables
private String name;
//Variables in parameters are local variables in methods
public void setName(String name){
    //this.name represents a member variable, and name represents a local variable
    this.name = name;
}

2. this calls member methods

Calling member methods with this also occurs within methods.Typically, when we call other methods within a method, we use a direct call, which is to write the method name directly.In fact, at this point, we can use the this keyword plus'."to call member methods before the method name to be called. The two ways of calling member methods are the same. Because the second way does not make much sense, in general, we choose the direct method.

public class ThisTest{
    public void method1(){
        System.out.println("Member Method");
    }
    public void method2(){
        //The following two methods are called in the same way
        method1();
        this.method1();
    }
}

3. this calls other overloaded constructors

Calling other overloaded constructors with this keyword is one of the most important uses of this keyword. Calling other construction methods with this requires that the this statement be written on the first line in the constructor, otherwise an error will be generated.The advantage of this method of invocation is that a constructor can provide a good class code structure by calling other constructors instead of repeating the code already in other constructors.

public class ThisTest{
    //Define two member variables
    private String name;
    private int height;
    //Parameterless constructor
    public ThisTest(){
        //Calling a constructor with a String type argument using this
        this("Ben");
    }
    //Constructor with String parameters
    public ThisTest(String name){
        //Use this to invoke constructors with parameters of type String and int
        this(name,165);
    }
    //Constructor with String and int parameters
    public ThisTest(String name, int height){
        //Assigning values to two attributes
        this.name = name;
        this.height = height;
    }
}
public class Test(){
    public static void main(String[] args){
        //create object
        new ThisTest();
    }
}

When an object is created, the constructor is called. Let's look at the order in which the constructor is called:
First, there are no parameters passed in when the object is created, so call the parameterless constructor first, enter the parameterless constructor, then execute this("Ben"), and then enter the parameterized constructor with a parameter of type String, pass "Ben" to name, execute this(name,165), and finally enter the constructor with parameters of type String and int, execute the statements within the constructor, that is, the final name is "Ben""The final height is 165.
This method of initializing member variables using the constructor with the most parameters is common in development.

4. this represents the current object

Direct Up Code

public class This2Test{
    //Define an attribute
    public String name;
    //Constructor with String parameters
    public This2Test(String name){
        this.name = name;
    }
    //Define a method whose parameter is a reference type
    public void method3(This2Test tt){
        //Output the properties of this object
        System.out.println(tt.name);
    }
    public void method4(){
        //Call the method and pass in this
        method3(this);
    }
}
public class Test{
    public static void main(String[] args){
        //Create Object t
        This2Test t = new This2Test("Baker");
        //Object t Call Method
        t.method4();
    }
}

The output is: Baker.
Let's analyze how the running program works and what does this represent?Why is Baker the output?
First look at the program's entry main method, first create the object t, when creating the object, enter the Test2This class to call its parameterized constructor, and then the name is the incoming "Baker", that is, the name of the object t currently created is "Baker".Then, the method4() method is called with object t, which calls the method3() method internally and passes in this.Therefore, enter the method3() method and print the name of this, where this represents the object calling the method4() method, which is the initial object t.So here's the fourth use of this -- this represents the current object.Here, you can simply understand that this is called in the method4() method, so this refers to the object that calls the method4() method.

Posted by Andrew B on Sun, 09 Jun 2019 11:43:02 -0700